在多个条件下在R中设置数据帧 [英] Subsetting a dataframe in R by multiple conditions
问题描述
data
v1 v2 v3 v4
avdc
avdd
bnpg
bddh
ckdc
crpg
dvdx
dvdc
evdb
evdc
如果列v1有一个b,d或e,我想摆脱这一行观察,产生以下数据框:
v1 v2 v3 v4
pre>
avdc
avdd
ckdc
crpg
我已经成功地摆脱了一个使用
sub.data< -data [data [,1]!=b,]
但是我有很多很多东西我想摆脱,所以一次做一个是不可取的。我没有成功执行以下操作:
sub.data< -data [data [,1]!= c b,d,e)
或
sub.data< -subset(data,data [,1]!= c(b,d,e))
我也尝试过其他一些事情,例如%$ / $ c中的
!% $ c>,但似乎不存在。
任何想法?解决方案
!
应该在声明外:data [!(%c中的数据$ v1%(b,d e))]]
v1 v2 v3 v4
1 avdc
2 avdd
5 ckdc
6 crpg
I would like to subset a dataframe by telling R what not to keep in the new dataframe. Here is a simplified sample dataframe:
data v1 v2 v3 v4 a v d c a v d d b n p g b d d h c k d c c r p g d v d x d v d c e v d b e v d c
If column v1 has a "b", "d", or "e", I want to get rid of that row of observations, producing the following dataframe:
v1 v2 v3 v4 a v d c a v d d c k d c c r p g
I have been successful at getting rid of one at a time using
sub.data<-data[data[,1] != "b",]
but I have many, many things I'd like to get rid of, so doing it one at a time is not desirable. I have not been successful with the following:
sub.data<-data[data[,1] != c("b","d","e")
or
sub.data<-subset(data, data[,1] != c("b","d","e"))
I've tried some other things as well, like
!%in%
, but that doesn't seem to exist. Any ideas?解决方案The
!
should be around the outside of the statement:data[!(data$v1 %in% c("b", "d", "e")), ] v1 v2 v3 v4 1 a v d c 2 a v d d 5 c k d c 6 c r p g
这篇关于在多个条件下在R中设置数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!