从嵌套列表的每个元素中删除特定值

问题描述 投票:0回答:1

我有一个嵌套列表,如下所示:

nested.list <- list(c(46270L, 103154L, 159944L, 193405L, 199925L), c(24049L, 
  30454L, 55710L, 106407L, 122059L, 174131L), c(14520L, 46270L, 
  153636L, 188626L, 199925L), c(8150L, 24049L, 27321L, 30461L, 
  33513L, 55710L, 58933L, 71342L, 103154L, 122059L, 159920L, 169516L, 
  174131L), c(19195L, 71333L, 122059L, 137645L, 153636L, 183740L, 
  195065L, 199925L), c(14520L, 60368L, 80939L, 82381L, 95070L, 
  103172L, 106379L, 147215L, 166353L, 199925L), c(30461L, 68324L, 
  75981L, 77674L, 106407L, 120284L), c(24029L, 72751L, 103154L, 
  120284L, 142359L))

> nested.list
[[1]]
[1]  46270 103154 159944 193405 199925

[[2]]
[1]  24049  30454  55710 106407 122059 174131

[[3]]
[1]  14520  46270 153636 188626 199925

[[4]]
 [1]   8150  24049  27321  30461  33513  55710  58933  71342 103154 122059 159920 169516 174131

[[5]]
[1]  19195  71333 122059 137645 153636 183740 195065 199925

[[6]]
 [1]  14520  60368  80939  82381  95070 103172 106379 147215 166353 199925

[[7]]
[1]  30461  68324  75981  77674 106407 120284

[[8]]
[1]  24029  72751 103154 120284 142359

在每个列表中,我只想保留这些数字。

target.values <- c(24030L, 33514L, 60369L, 106408L, 147216L, 153637L, 159921L, 
                   193406L)

我尝试过

purrr::keep(.x = nested.list, .p = function(x){all(x %in% target.values)})
,但显然我错过了一些关于它是如何工作的。

r purrr nested-lists
1个回答
0
投票
map(nested.list, keep, \(x) x %in% target.values)
© www.soinside.com 2019 - 2024. All rights reserved.