Having a dataframe like this
df <- data.frame(ID = sample(rep(letters, each=3)), value = rnorm(n=26*3)) keep <- c("a", "d", "r", "x")
How is it possible to keep both "a" and "d"
example using something like this: df[df$ID == "a"|"d", ]
df[df$ID == "a"|"d", ]
Use %in%
%in%
df[df$ID %in% c("a","d"), ]
We can use base R function which(). which() returns indexes where given condition is met.
which()
df[which(df$ID == "a" | df$ID == "d"),]
An option with %chin%
%chin%
library(data.table) setDT(df)[ID %chin% c('a', 'd')]