1

用户,

我有这个数据框:

A<- c(10,2,4,5,3,5,98,65,36,65,6,100,70,54,25,23,22,30,15,23)
B<- c(1,0.1,0.5,0.8,0.2,0.9,3,1.2,5.6,3.5,15.9,10.2,5,5.1,7.1,5,6,10,4,8)
C<- c("a","a","a","a","a","a","b","b","b","b","c","c","c","c","d","d","d","d","d","d")
mydf<- data.frame(A,B,C)

我做了一个只保留“a”级别的子集。

subset<- subset(mydf, mydf$C=="a")

但是当我制作一个图(请参见图片)时,该图也显示了已删除的级别。

plot(B~ C, data=subset)

如何绘制子集数据框以避免删除级别?

绘制子集数据

谢谢!

4

3 回答 3

3
str(subset)
#'data.frame':   6 obs. of  3 variables:
# $ A: num  10 2 4 5 3 5
# $ B: num  1 0.1 0.5 0.8 0.2 0.9
# $ C: Factor w/ 4 levels "a","b","c","d": 1 1 1 1 1 1

通过 删除缺失的因子水平factor

subset$C <- factor(subset$C)

str(subset)
#'data.frame':   6 obs. of  3 variables:
#$ A: num  10 2 4 5 3 5
#$ B: num  1 0.1 0.5 0.8 0.2 0.9
#$ C: Factor w/ 1 level "a": 1 1 1 1 1 1
于 2013-10-21T13:42:21.083 回答
2

使用droplevels

subset$C <- droplevels(subset$C)
plot(B~ C, data=subset)

顺便说一句,subset对于 data.frame 来说,这不是一个好名字。

于 2013-10-21T13:36:36.437 回答
1

做就是了:

plot(B~ droplevels(C), data=subset)
于 2013-10-21T13:36:17.460 回答