Data.frame成为过滤/子集后的因子/向量 [英] Data.frame becomes factor/vector after filtering/subsetting

查看:89
本文介绍了Data.frame成为过滤/子集后的因子/向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框架,一列如下:

I have a data.frame with 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"

这里发生了什么?

推荐答案

欢迎你刚刚被 drop 烦恼所困:你需要明确告诉R不要放到一维:

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] 

这篇关于Data.frame成为过滤/子集后的因子/向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆