Removing multiple values in R

I have a dataset with like 14,000 rows and 40 columns I'm trying to remove all the rows with the below values in the first column of the data set, but when I do something like

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")

It doesn't work. Is there an easy way to do this or do I have to make a function?

Answer #1:

You can also use subset:

subset(data_set, ! DMS %in% rem)
Answered By: Zelazny7

Answer #2:

Or this (which makes it clear you filtering on rows):

data_set[!data_set$DMS %in% rem,]
Answered By: fishtank

Answer #3:

You need filter(data_set, ! DMS %in% rem)

example:

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
Answered By: Ben Bolker

Answer #4:

Using data.table, we set the 'key' column

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

Using the example from @Ben Bolker's post

rem <- c('a', 'c', 'e')
setDT(dd, key='f')[!rem]
#   f x
#1: b 2
#2: d 4
#3: f 6
Answered By: akrun
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .



# More Articles