Jake Jake - 2 months ago 5x
R Question

Removing rows from a dataframe based on a large list

I have a long list of values which I want to use as indexing to remove rows from another data frame which contains this values at a specific column.

A simple examle to make it is

X <- data.frame(Variable1=c(11,14,12,15),Variable2=c(2,3,1,4))
X[X$Variable1!=11 & X$Variable1!=12, ]

but is there any efficient way to use the dataframe with the values I have which is the list I want to use in the second dataframe?


We can use %in% for creating a logical index based on multiple values.

X[!(X$Variable1 %in% c(11, 12)),]

If we are using data.table an option would be to set the key and then negate the values

setDT(X, key = "Variable1")[!.(c(11,12))]
#     Variable1 Variable2
#1:        14         3
#2:        15         4

Or we use on as @David Arenburg commented

setDT(X)[!.(11:12), on = "Variable1"]