Home > other >  How can I subset data from a data.frame in R
How can I subset data from a data.frame in R

Time:01-26

Let's assume I have the following:

df = data.frame(attribute_1 = c(234, 456, 778,  89,  77, 665,  44,  33),
                attribute_2 = c( 9,67,78,777,6, 1, 22, 100))

vec = c(1,44,33,667,77)

subset(df, df$attribute_1 != vec)

how can I exclude the values in vec from the df?

The code as it is written here gets an error message

CodePudding user response:

Instead of !=, use %in% with ! as != or == are elementwise comparison operators which works only when the lengths are same or if it is having a length of 1 which gets recycled

subset(df, ! attribute_1 %in% vec)
  attribute_1 attribute_2
1         234           9
2         456          67
3         778          78
4          89         777
6         665           1

CodePudding user response:

You can either do

subset(df, ! attribute_1 %in% vec)

or (using package tidyverse)

df %>% filter(!attribute_1 %in% vec)

or simply

df[!df$attribute_1 %in% vec, ]
  •  Tags:  
  • Related