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

青春壹個敷衍的年華 提交于 2019-11-26 17:03:53

问题


I have a data frame with an ID column and a few columns for values. I would like to only keep certain rows of the data frame based on whether or not the value of ID at that row matches another set of values (for instance, called "keep").

For simplicity, here is an example:

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

How can I create a new data frame consisting of rows that only have IDs that match those of keep? I can do this for just one letter by using the which() function, but with multiple letters I get warning messages and incorrect returns. I know I could run a for loop through the data frame and extrapolate that way, but I'm wondering if there is a more elegant and efficient way of going about this. Thanks in advance.


回答1:


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

Edit: Also, if this were for a single letter, you could write e.g. df[df$ID == "a", ] instead of using which().



来源:https://stackoverflow.com/questions/19200188/keeping-only-certain-rows-of-a-data-frame-based-on-a-set-of-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!