I have a list with vectors of different length, for example:
a=c(12345,12367,91670,87276,92865)
b=c(12345,87276,89250)
c=c(12367,91670)
d=c(23753,82575,91475,10957,92865,24311)
mylist=list(a,b,c,d)
mylist
# [[1]]
# [1] 12345 12367 91670 87276 92865
#
# [[2]]
# [1] 12345 87276 89250
#
# [[3]]
# [1] 12367 91670
#
# [[4]]
# [1] 23753 82575 91475 10957 92865 24311
my question is how can I remove the vectors of this list which are a subset of another vector of the same list. i.e. in this case how can I remove the 3rd object of the list which is a subset of the 1st object??
This gives a new list without the elements that are subsets of others...
newlist <- mylist[!sapply(seq_along(mylist), function(i) max(sapply(mylist[-i],function(L) all(mylist[[i]] %in% L))))]
This is may be pretty inefficient, but if your list isn't that big it could work
find_nested <- function(mylist) {
mm <- sapply(mylist, function(x) sapply(mylist, function(y) all(x %in%y)))
diag(mm) <- FALSE
apply(mm,2,any)
}
This can tell you which vectors are subsets of other vectors. It does this by comparing every vector to every other vector.
find_nested(mylist)
# [1] FALSE FALSE TRUE FALSE
So we can see the third item is contained in another list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With