通过删除行的多个逻辑条件对数据框进行子集设置 [英] Subset dataframe by multiple logical conditions of rows to remove

查看:52
本文介绍了通过删除行的多个逻辑条件对数据框进行子集设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过指定哪些行 not )保留在新数据框中来对数据框进行子集化(过滤)。这是一个简化的示例数据框:

I would like to subset (filter) a dataframe by specifying which rows 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

例如,如果第v1列的行包含 b, d或 e,则我想摆脱该行的观察值,并生成以下数据框: / p>

For example, if a row of 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

我一次成功地基于一个条件进行了子集化。例如,在这里我删除了v1包含 b的行:

I have been successful at subsetting based on one condition at a time. For example, here I remove rows where v1 contains a "b":

sub.data <- data[data[ , 1] != "b", ]

但是,我有很多这样的条件,所以一次一次不理想。我没有成功完成以下操作:

However, I have many, many such conditions, 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")

sub.data <- subset(data, data[ , 1] != c("b", "d", "e"))

我也尝试了其他操作,例如!%in%,但似乎不存在。
有什么想法吗?

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

这篇关于通过删除行的多个逻辑条件对数据框进行子集设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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