根据一组值仅保留数据框

根据一组值仅保留数据框

本文介绍了根据一组值仅保留数据框的某些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 ID 列和几列值的数据框.我只想根据该行的 ID 值是否与另一组值(例如,称为keep")匹配来保留数据框的某些行.

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")

如何创建一个新的数据框,其中包含的行的 ID 与 keep 的 ID 匹配?我可以通过使用 which() 函数只对一个字母执行此操作,但是对于多个字母,我会收到警告消息和不正确的返回值.我知道我可以通过数据框运行 for 循环并以这种方式进行推断,但我想知道是否有更优雅、更有效的方法来解决这个问题.提前致谢.

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.

推荐答案

Try df[df$ID %in% keep, ]subset(df, ID %in% keep) -- 查看集合的帮助页面.

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

另外,如果这是单个字母,您可以写例如df[df$ID == "a", ] 而不是使用 which().

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

这篇关于根据一组值仅保留数据框的某些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:17