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

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

问题描述

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

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",我想去掉那一行观察,生成以下数据框:

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?

推荐答案

! 应该在语句的外面:

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天全站免登陆