我正在处理如下所示的列表:
p
$AAANWWTGC_UNKNOWN
[1] "4208" "481" "6095" "10370" "351"
$AAAYRNCTG_UNKNOWN
[1] "4052" "9842" "23047" "55800" "1942"
$MYOD_01
[1] "23630" "84668" "3151" "26523" "667"
$E47_01
[1] "90523" "84668" "26523" "154807" "1942"
我想过滤p以获得另一个列表,比如说q,只保留列表p中包含给定向量中至少一个字符的元素,例如:
v<-c("4052","1942")
因此,此特定示例中的新列表q应如下所示:
q
$AAAYRNCTG_UNKNOWN
[1] "4052" "9842" "23047" "55800" "1942"
$E47_01
[1] "90523" "84668" "26523" "154807" "1942"
非常感谢!
答案 0 :(得分:3)
我们可以使用Filter
base R
Filter(function(x) any(v %in% x), p)
#$AAAYRNCTG_UNKNOWN
#[1] "4052" "9842" "23047" "55800" "1942"
#$E47_01
#[1] "90523" "84668" "26523" "154807" "1942"
答案 1 :(得分:1)
您可以尝试使用purrr
中包含的keep
tidyverse
函数计算匹配数。然后对列表中的0
进行子集化。
library(tidyverse)
list(c( "4208" , "481" ,"6095" ,"10370" ,"351" ),
c( "4052" , "9842", "23047", "55800" , "1942"),
c("23630", "84668" , "3151" ,"26523", "667"),
c("90523" , "84668" , "26523" , "154807" ,"1942" )) %>%
keep(function(x) sum(x %in% v) > 0)
[[1]]
[1] "4052" "9842" "23047" "55800" "1942"
[[2]]
[1] "90523" "84668" "26523" "154807" "1942"