Saving only certain rows of a data frame based on a set of values

I have a data frame with an ID column and several columns for the values. I would like to save only certain rows of the data frame based on whether the ID value in this row matches a different set of values ​​(for example, called "hold").

For simplicity, an example is given:

df <- data.frame(ID = sample(rep(letters, each=3)), value = rnorm(n=26*3)) keep <- c("a", "d", "r", "x") 

How to create a new data frame consisting of lines that have only identifiers corresponding to keep files? I can only do this for one letter using the which() function, but with a few letters I get warning messages and incorrect returns. I know that I can run a for loop through a data frame and extrapolate this path, but I wonder if there is a more elegant and efficient way around this. Thanks in advance.

+6
source share
1 answer

Try df[df$ID %in% keep, ] or subset(df, ID %in% keep) - see the sets help page.

Edit: Also, if it was for a single letter, you can write, for example. df[df$ID == "a", ] instead of which() .

+17
source

Source: https://habr.com/ru/post/955304/


All Articles