-1

Imagine a dataset like this

Dataset

Var1 Var2 
a    apple
b    banana
c    table
d    apple
e    banana
f    table
g    banana

I have a vector like this

x<-c("apple","banana")

I want to subset the dataset and obtain a dataset that only has elements which Var2 is equal to ONE of the elements of x.

so that the output should be a dataset like

Var1 Var2 
a    apple
b    banana
d    apple
e    banana
g    banana

I know the question might seem trivial, but I can't find an answer on the web.

pogibas
  • 27,303
  • 19
  • 84
  • 117

1 Answers1

0
## lets say dataset df , has var1, var2
df[df$Var2 %in% x, ]
## %in% is vectorized, so it will essentially give a T/F for every x%in%y

Hence when you do data.frame[True/Fase, ] , it gives all rows of DF that have TRUE.

Yash
  • 307
  • 3
  • 10