Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove vectors which are subsets of other vectors in a list

Tags:

r

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??

like image 926
P. Z Avatar asked Mar 28 '17 18:03

P. Z


2 Answers

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))))]
like image 190
Andrew Gustar Avatar answered Nov 15 '22 05:11

Andrew Gustar


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.

like image 43
MrFlick Avatar answered Nov 15 '22 06:11

MrFlick