开发者

Data.frame becomes factor/vector after filtering/subsetting

开发者 https://www.devze.com 2023-02-08 08:26 出处:网络
I have a data.frame wi开发者_StackOverflow社区th one column, like so: >d = data.frame(animal=c(\"horse\",\"dog\",\"cat\"))

I have a data.frame wi开发者_StackOverflow社区th one column, like so:

>d = data.frame(animal=c("horse","dog","cat"))

then I filter it by excluding all items also present in a vector. e.g.:

> res = d[!(d$animal %in% c("horse")),]
> res
[1] dog cat
Levels: cat dog horse
>class(res)
[1] "factor"

What is going on here?


Welcome to R. You've just been bitten by the drop annoyance: you need to explicitly tell R not to "drop to one-dimension":

res = d[!(d$animal %in% c("horse")), , drop = FALSE] 
0

精彩评论

暂无评论...
验证码 换一张
取 消