# [R] Union of list elements

Jean Eid jeaneid at chass.utoronto.ca
Sat Dec 18 14:26:12 CET 2004

```Can't you turn the lists into data frames issue unique and
force them back to lists. Here's the code:

L <- list(c("a1","a3","a4"), c("a1","a4","a5"), c("a1","a5","a6"))
M <- list(c("a1","a3","a4"), c("a2","a4","a5"), c("a1","a5","a6"), c("a7",
"a1", "a4"))
LL <- as.data.frame(I(L))
MM <- as.data.frame(I(M))
X <- as.list(unique(rbind(LL,MM)))

You have an issue here that which needs to be addressed;
the ordering of the elements within each list. Is this suppose to be
informative? i.e. do you care that you have ("a1", "a2", "a3") versus
("a2", "a3", "a1"). If not the above solution has to be redesigned. as an
example:

L <- list(c("a1","a3","a4"), c("a1","a4","a5"), c("a1","a5","a6"))
M <- list(c("a1","a3","a4"), c("a2","a4","a5"), c("a1","a6","a5"))
LL <- as.data.frame(I(L))
MM <- as.data.frame(I(M))
X <- as.list(unique(rbind(LL,MM)))

Above will generate ("a1","a6","a5"), and ("a1","a5","a6") as two
distinct rows. However, sorting them first will give you what you want
(again if this is uninformative)

L <- lapply(L, sort)
M <- lapply(M,sort)
LL <- as.data.frame(I(L))
MM <- as.data.frame(I(M))
X <- as.list(unique(rbind(LL,MM)))

P.S In your last email you stated that you have two sets .... I think
list and you want the unique sets of list N, however
what you have is a huge list that you cannot issue the command unique.
That is why you are creating L and that is why what Gabor suggested does
not  work initially because the list L is starting empty. But if that is
the  case all you have to do is turn the list into data.frame issue unique
and turn it back to a list

M <- list(c("a1","a3","a4"), c("a2","a4","a5"), c("a1","a6","a5"),
c("a1","a6","a5"))
MM <- as.data.frame(I(M))
X <- as.list(unique(MM))

Hope this helps,

Jean,

On Sat, 18 Dec 2004, Patrick Meyer wrote:

> Thanx to all of you who helped me with my sets problem. ;-)
>
> Patrick
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help