CadisEtRama
CadisEtRama

Reputation: 1111

R: excluding rows with a preset list of values in a data frame

I have a data frame that is very large but very similar to this:

df <- data.frame(Group = rep(c('A', 'B', 'C', 'D'), 50),
                 Number = sample(1:100, 200, replace = T))

Group   Number
A   52
B   74
C   22
D   90
A   7
B   93
C   50
D   10
A   31
B   19

I have another data frame named "remove" that looks like this:

>remove
 Group  Number
    A   52
    C   22
    B   93
    D   10

How can I subset the df data so I exclude all rows with the Group and Number values in "remove" to get the following data frame? The file is very large so I would be unable to manually type in the values I want to exclude. Desired output:

Group   Number
B   74
D   90
A   7
C   50
A   31
B   19

Thanks!

Upvotes: 0

Views: 13443

Answers (2)

Christopher Louden
Christopher Louden

Reputation: 7582

You can do it with %in%

df <- df[!(df$Number %in% remove$Number),]

%in% will return TRUE if df$Number is in the vector remove. Since we want the complement, we negate the set first with !.

Upvotes: 7

b123kotey
b123kotey

Reputation: 31

For more complex exclusions you could create a unique column in both dataframes (ie Group+Number) and seek to exclude that as per the method described by Christopher.

Upvotes: 0

Related Questions