我有一个只有一列的数据框,如下所示:
>d = data.frame(animal=c("horse","dog","cat"))
然后我通过排除向量中存在的所有项目来对其进行过滤。例如:
> res = d[!(d$animal %in% c("horse")),]
> res
[1] dog cat
Levels: cat dog horse
>class(res)
[1] "factor"
这里发生了什么事情?
欢迎来到R语言。您刚刚遇到了一个名叫drop
的烦恼: 您需要明确告诉R不要“降为一维”:
res = d[!(d$animal %in% c("horse")), , drop = FALSE]
class(d[[1]])
。 - Joshua Ulrich