1

我有很多数据框,我必须按序数李克特尺度变量进行过滤。而不是选择它们x == "Strongly disagree" | x == "Disagree"等等,我想选择它们x < 3。我怎样才能做到这一点?

xl <- sample(1:5, 20, replace = T)
x <- factor(xl,labels=c("Strongly disagree","Disagree","Neither agree nor disagree","Agree", "Strongly agree"),ordered=TRUE)
y<-sample(2:40,20)
z<-data.frame(x,y)
a<-subset(z,x == "Strongly disagree" | x == "Disagree")
a

谢谢

4

3 回答 3

4

有序因子知道它们是有序的,所以您只需要:

x < "Agree"
x >= "Disagree"

ETC

于 2013-09-05T20:15:35.333 回答
1

像这样的东西?

z[as.numeric(z$x) < 3, ]
于 2013-09-05T20:01:50.810 回答
0
levels(x)<-seq(1,length(levels(x)),1) #and then do subsetting
z<-data.frame(x,y)
> a<-subset(z,x<3)
> a
   x  y
1  1 12
2  2  5
3  1  8
4  2  9
5  2 15
9  1 33
10 2 27
11 2 13
18 1 11

注:1 表示非常不同意,2 表示不同意,依此类推

于 2013-09-05T19:59:18.177 回答