问题
I have the following list of vectors:
a <- c(1,2,4,5,6,7,8,9)
b <- c(1,2,4,5)
c <- c(1,2,3,10,11,12,13,14)
d <- c(1,2,3,10,15,16,17,18,19)
e <- c(1,2,3,10,15,16)
f <- list(a,b,c,d,e)
Right now, I can do something like this
is_subset <- vector()
for(i in 1:length(f)) {
is_subset <- c(is_subset, all(unlist(f[i]) %in% unlist(f[-i])))
}
f[!is_subset]
and get a list containing every vector that is not a subset of any other vector from the original list:
[[1]]
[1] 1 2 4 5 6 7 8 9
[[2]]
[1] 1 2 3 10 11 12 13 14
[[3]]
[1] 1 2 3 10 15 16 17 18 19
However, what I really want is to subset the list so that it includes only those vectors that are not supersets of other vectors from the list -- i.e., the desired output should look like this:
[[1]]
[1] 1 2 4 5
[[2]]
[1] 1 2 3 10 11 12 13 14
[[3]]
[1] 1 2 3 10 15 16
How can I do this in R?
回答1:
In the code snippet below, I compare each vector in your list against every other vector using %in%
. If the sum of each comparison vector comes up non zero more than once, then the vector is a superset of another vector. Note that we expect a single comparison to match exactly, namely the comparison of a vector against itself.
out <- sapply(f, function(x) {
res <- lapply(f, function(y) {
return(sum(!(y %in% x)))
})
return(sum(res > 0) == length(res) - 1)
})
f[out]
[[1]]
[1] 1 2 4 5
[[2]]
[1] 1 2 3 10 11 12 13 14
[[3]]
[1] 1 2 3 10 15 16
Demo
来源:https://stackoverflow.com/questions/47244949/check-if-a-vector-is-a-superset-of-another-vector-in-r