删除R中的多个值

时间:2015-12-31 21:30:25

标签: r

我有一个包含14,000行和40列的数据集我正在尝试删除数据集第一列中具有以下值的所有行,但是当我执行类似

的操作时
filter(data_set, data_set$DMS != rem)


rem <- c("02M177","02M267", "02M933","03M452","05M148","06M178","06M209","07X359","09X274","09X294","09X311","09X350","09X361","09X555","11X355","12X314","14K414","17K532","18K763","19K404","19K557","19K654","19K661","19K662","19K663","19K760","20K264","20K971","23K446","23K599","23K664","23K668","24Q290","24Q311","24Q330","27Q273","27Q297","27Q362","28Q287","28Q332","29Q289","30Q280","30Q291","30Q300","31R028","31R078")

它不起作用。有没有一种简单的方法可以做到这一点,还是我必须做一个功能?

4 个答案:

答案 0 :(得分:3)

您还可以使用subset

subset(data_set, ! DMS %in% rem)

答案 1 :(得分:2)

您需要filter(data_set, ! DMS %in% rem)

示例:

dd <- data.frame(f=letters[1:6],x=1:6)
library("dplyr")
dd %>% filter(!f %in% c("a","c","e"))
##   f x
## 1 b 2
## 2 d 4
## 3 f 6

答案 2 :(得分:2)

或者这个(这清楚地表明你对行进行过滤):

data_set[!data_set$DMS %in% rem,]

答案 3 :(得分:1)

使用data.table,我们设置了&#39;键&#39;专栏

library(data.table)
setDT(data_set, key='DMS')[!rem]

使用@Ben Bolker的帖子中的例子

rem <- c('a', 'c', 'e')
setDT(dd, key='f')[!rem]
#   f x
#1: b 2
#2: d 4
#3: f 6