R 如何将一个向量拆分为因子以进行方框图?
我有一套收入数据集。我想根据种族显示收入的箱线图 比赛分为0到10个数字。 0到3是白色,4到5是黑色,6到10是混合色 如何根据种族显示收入的箱线图 我尝试将其拆分为因子,现在我使用了3个因子:R 如何将一个向量拆分为因子以进行方框图?,r,R,我有一套收入数据集。我想根据种族显示收入的箱线图 比赛分为0到10个数字。 0到3是白色,4到5是黑色,6到10是混合色 如何根据种族显示收入的箱线图 我尝试将其拆分为因子,现在我使用了3个因子: white <- factor(Race < 4) black <- factor(Race>4 & Race<6) mixed <- factor(Race>6) white您可以使用cut Race = 0:10 R2 = factor(cut(
white <- factor(Race < 4)
black <- factor(Race>4 & Race<6)
mixed <- factor(Race>6)
white您可以使用cut
Race = 0:10
R2 = factor(cut(Race, breaks=c(0,3,5,10), include.lowest=TRUE),
labels=c("White", "Black", "Mixed"))
R2
[1] White White White White Black Black Mixed Mixed Mixed Mixed Mixed
Levels: White Black Mixed
您可以使用cut
Race = 0:10
R2 = factor(cut(Race, breaks=c(0,3,5,10), include.lowest=TRUE),
labels=c("White", "Black", "Mixed"))
R2
[1] White White White White Black Black Mixed Mixed Mixed Mixed Mixed
Levels: White Black Mixed
使用dplyr:
levels <- c(3, 5, 10)
labels <- c("White", "Black", "Mixed")
data %>%
mutate(Race.factor = cut(Race, levels, labels = labels)) %>%
ggplot(aes(x=Race.factor, y=earnings) +
geom_boxplot()
使用dplyr:
levels <- c(3, 5, 10)
labels <- c("White", "Black", "Mixed")
data %>%
mutate(Race.factor = cut(Race, levels, labels = labels)) %>%
ggplot(aes(x=Race.factor, y=earnings) +
geom_boxplot()
我认为OP想要boxplot(收益~R2)
我认为OP想要boxplot(收益~R2)