Delete items in a list in R

I want to delete part of the list, where it is a complete set of another part of the list. For example, B intersects A and E intersecting C, so B and E should be deleted.

MyList <- list(A=c(1,2,3,4,5), B=c(3,4,5), C=c(6,7,8,9), E=c(7,8))
MyList
$A
[1] 1 2 3 4 5
$B
[1] 3 4 5
$C
[1] 6 7 8 9
$E
[1] 7 8

MyListUnique <- RemoveSubElements(MyList)
MyListUnique
$A
[1] 1 2 3 4 5
$C
[1] 6 7 8 9

Any ideas? Does any function know this?

+4
source share
2 answers

While your data is not too large, you can use an approach such as:

# preparation
MyList <- MyList[order(lengths(MyList))]
idx <- vector("list", length(MyList))
# loop through list and compare with other (longer) list elements
for(i in seq_along(MyList)) {
  idx[[i]] <- any(sapply(MyList[-seq_len(i)], function(x) all(MyList[[i]] %in% x)))
}
# subset the list
MyList[!unlist(idx)]        
#$C
#[1] 6 7 8 9
#
#$A
#[1] 1 2 3 4 5
+1
source

Similar to another answer but hopefully clearer using helper function and 2 sapplys.

#helper function to determine a proper subset - shortcuts to avoid setdiff calculation if they are equal
is.proper.subset <- function(x,y) !setequal(x,y) && length(setdiff(x,y))==0

#double loop over the list to find elements which are proper subsets of other elements
idx <- sapply(MyList, function(x) any(sapply(MyList, function(y) is.proper.subset(x,y))))

#filter out those that are proper subsets
MyList[!idx]
$A
[1] 1 2 3 4 5

$C
[1] 6 7 8 9
+1
source

Source: https://habr.com/ru/post/1671593/


All Articles