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", ]
We can use base R function which()
. which()
returns indexes where given condition is met.
df[which(df$ID == "a" | df$ID == "d"),]
An option with %chin%
library(data.table)
setDT(df)[ID %chin% c('a', 'd')]