我有一个由“数据”定义的数据框和该数据集的两个子集(scale1、scale2)。我想按性别然后按国家来绘制这个比例,而不必每次都定义比例。我知道这在 R 中是可能的,并且可能是一个新手问题,但我就是找不到这样做的逻辑。我已经搜索过了,但我找不到我想要的。如果有人可以为我提供有关数据管理的线索(而不是用于绘图),我将不胜感激。
我知道使用那个简单的 DataFrame 可能每次都更容易定义子集,但是我原来的 DataFrame 有很多项目,很难以这种方式操作。
Data<- data.frame(item1=c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, NA, 5, NA, NA),
item2=c(1, 2, 2, 4, 1, 1, 2, 3, 5, 5, NA, NA, NA, NA),
item3=c(1, 2, 2, 4, 1, 1, 2, 3, 5, 5, NA, NA, NA, NA),
item4=c(1, 2, 2, 4, 1, 1, 4, 3, 1, 5, NA, 3, NA, NA),
item5=c(1, 5, 2, 4, 2, 1, 2, 3, 5, 5, NA, NA, 1, NA),
item6=c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, NA, 5, NA, NA),
item7=c(1, 2, 1, 5, 1, 2, 2, 3, 5, 5, NA, NA, NA, NA),
item8=c(1, 4, 2, 4, 3, 1, 2, 8, 5, 5, NA, NA, NA, NA),
gender=c(1, 2, 1, 2, 2,1, 2, 1, 2, 1, 1, 2, 1, 2),
country=c(1, 2, 3, 3, 1,1, 2, 1, 3, 1, 3, 2, 1, 2))
scale1 <- subset(Data, select=c(item1, item2, item3, item4))
scale2 <- subset(Data, select=c(item5, item6, item7, item8))
现在为了绘图,我正在使用这个指令,但我很确定还有另一种更好的方法可以做到这一点:
womandata <- Data[ which(Data$gender== "1"), ]
scale1F <-subset(womandata, select= c( item1, item2, item3, item4))
scale2F <-subset(womandata, select= c( item5, item6, item7, item8))
mandata <- Data[ which(Data$gender== "2"), ]
scale1M <-subset(mandata, select= c( item1, item2, item3, item4))
scale2M <-subset(mandata, select= c( item5, item6, item7, item8))
par(mfrow=c(2,1))
boxplot(scale1F, xlab="", xaxt = "n", col="gray", main="Woman")
text(1:34, par("usr")[1],
srt=45, pos=1, xpd=TRUE, offset=-1)
boxplot(scale1M, xlab="", xaxt = "n", col="gray", main ="Man")
text(1:34, par("usr")[1],
srt=45, pos=1, xpd=TRUE, offset=-1)
最好的祝愿,
安古洛