[英]How to reduce (subset) list of lists?
I have a list of lists like this:我有一个像这样的列表:
list.ex <- list('a' = c(10,11), 'b' = c(9,10,11), 'c' = c(8,9,10,11), 'd' = c(7,8,9,10,11), 'e' = c(6,7,8,9,10,11), 'f' = c(6,8,9,10,11))
I want to check and remove those lists which are a subset of another list (in the exact order).我想检查并删除作为另一个列表子集的那些列表(按确切顺序)。 eg In this case I want to reduce list.ex
such that we are left with list.ex[[e]]
and list.ex[[f]]
.例如,在这种情况下,我想减少list.ex
以便我们留下list.ex[[e]]
和list.ex[[f]]
。
is_subsequence
determines if a
is a subset of b
in the same order. is_subsequence
确定a
是否是b
的相同顺序的子集。
library(tidyverse)
is_subsequence <- function(a, b) {
str_detect(
paste0(b, collapse = ","),
fixed(paste0(a, collapse = ","))
)
}
matches_other
takes a vector and determines if it matches more than one vector in list.ex
matches_other
接受一个向量并确定它是否与list.ex
中的多个向量list.ex
matches_other <- function(l) {
map_lgl(
list.ex,
~is_subsequence(l, .)
) %>%
sum() %>%
`>`(1)
}
discard
is used to remove matches. discard
用于删除匹配项。
list.ex %>%
discard(matches_other)
#> $e
#> [1] 6 7 8 9 10 11
#>
#> $f
#> [1] 6 8 9 10 11
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.