An alternative (from the list) for elements is the "by value" table:
table(values = unlist(mylist), elt = rep(seq_along(mylist), lengths(mylist)))
It can very easily consume a lot of memory so that we can pursue a rare alternative:
l = unlist(mylist) ul = unique(l) tab = sparseMatrix(x = TRUE, i = match(l, ul), j = rep(seq_along(mylist), lengths(mylist)), dimnames = list(ul, sprintf("elt_%d", seq_along(mylist)))) tab #11 x 4 sparse Matrix of class "lgCMatrix" # elt_1 elt_2 elt_3 elt_4 #12345 | | . . #12367 | . | . #91670 | . | . #87276 | | . . #92865 | . . | #89250 . | . . #23753 . . . | #82575 . . . | #91475 . . . | #10957 . . . | #24311 . . . |
Then, to find which element is a subset of which:
subsets = lengths(mylist) == crossprod(tab) subsets #4 x 4 sparse Matrix of class "lgCMatrix" # elt_1 elt_2 elt_3 elt_4 #elt_1 | . . . #elt_2 . | . . #elt_3 | . | . #elt_4 . . . |
where here each element is a subset of itself ... and 3 is a subset of 1. To get the information we need, we could use:
subset(summary(subsets), i != j)[c("i", "j")]
Or, to avoid including diag indices and their subsets later, we could manipulate an existing structure:
dp = diff( subsets@p ) j = rep(0:(length(dp) - 1), dp) wh = subsets@i != j cbind(subset = subsets@i [wh], of = j[wh]) + 1L
In both of the latter cases, the unique first column shows which elements are subsets of the other and can be used for [ "mylist".