简体   繁体   English

select 行与 R 数据框中列表中的匹配项的快速方法

[英]Quick way to select rows with matching terms in a list in data frame in R

I have a data frame that has lists of lists that stores IDs:我有一个数据框,其中包含存储 ID 的列表:

a <- list(as.character(c("1","2","3")))
b <- list(as.character(c("2","3","5")))
c <- list(as.character(c("4","6","8")))
df = data.frame(NAME = c("A1", "A2", "A3"), stat = c(14, 15, 16)) 
df$IDs[1] <- a      
df$IDs[2] <- b   
df$IDs[3] <- c

Additionally, I have a list of characters which is a reference of IDs of my interest that I want to track:此外,我有一个字符列表,它是我想要跟踪的我感兴趣的 ID 的参考:

x <- list(as.character(c("2","3")))

I would like to filter the initial data frame so that it will only contain the rows that have IDs of 2 and/or 3 in the ID column of the data frame (ie, x matching to df$ID; thereby in this case only the rows named A1 and A2 in this case).我想过滤初始数据框,使其仅包含数据框 ID 列中 ID 为 2 和/或 3 的行(即 x 与 df$ID 匹配;因此在这种情况下,只有在这种情况下名为 A1 和 A2 的行)。

The actual data frame has hundreds of rows so I would appreciate a shorter route than a loop if possible.实际的数据框有数百行,所以如果可能的话,我会欣赏比循环更短的路线。
If you have a different approach as part of your suggestions (like wrangling the initial df a bit more), I'd also appreciate hearing them as well.如果您有不同的方法作为您的建议的一部分(比如更多地争论最初的 df ),我也很高兴听到它们。

Many thanks in advance.提前谢谢了。

Using tidyverse使用tidyverse

library(dplyr)
library(purrr)
df %>%
    filter(map_lgl(IDs, ~ any(unlist(x) %in% .x)))
  NAME stat     IDs
1   A1   14 1, 2, 3
2   A2   15 2, 3, 5

You could use sapply or mapply :您可以使用sapplymapply

df[sapply(df$IDs, \(a) any(x[[1]] %in% a)), ]
df[mapply(\(a, b) any(a %in% b), x, df$IDs), ]
Output Output
#   NAME stat     IDs
# 1   A1   14 1, 2, 3
# 2   A2   15 2, 3, 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM