I have a data frame df with an ID column eg A,B,etc. I also have a vector containing certain IDs:
L <- c("A", "B", "E")
How can I filter the data frame to get only the IDs present in the vector? Individually, I would use
subset(df, ID == "A")
but how do I filter on a whole vector?
You can use the
%in%operator:If your IDs are unique, you can use
match():or make them the rownames of your dataframe and extract by row:
Finally, for more advanced users, and if speed is a concern, I’d recommend looking into the
data.tablepackage.