[R] Union of list elements

Gabor Grothendieck ggrothendieck at myway.com
Sat Dec 18 01:16:04 CET 2004

Patrick Meyer <patrick.meyer <at> internet.lu> writes:

: It does not exactly do what it is meant to... Instead of breaking down, 
: my code is looping forever now... I think I will have a deeper look at 
: it tomorrow...
: In fact, I have two lists: let's say L and N.
: An elements (or set) of N must be added to L, if and only if it is not a 
: subset of an element of L.
: I think that the code you wrote does that, for each element of separately.

If both arguments are lists, not a list and vector as you previously
described, then just lapply over the previous formula (here encapsulated
in the function) using a second outer lapply:

L <- c(L, N[unlist(lapply(N, function(x) 
               !any(unlist(lapply(L, setequal, x)))))])

: Couldn't this be a solution: add all the elements of N to L, and 
: afterwards eliminate the sets which are present more than once? How 
: could one eliminate these sets?

Yes but it would involve unnecessary comparisons between pairs in
L as well as unnecessary comparisons between pairs in N.

More information about the R-help mailing list