# Finding union of two sets of list

Hello,

I have two sets of lists, with three lists in each set. Now my goal is to find the union (both common and uncommon) of the two sets of lists.

``````l1 <- list(c(1, 2, 5), c(1,5), c(1, 2, 5))
l2 <- list(c(1,  4,  5, 16, 31, 40, 50), c(1,  4,  5, 39), c(1,  2,  5, 28, 34, 40, 45, 49))
``````

The output should look like
[] 1 2 5
[] 1 3 5
[] 1 2 5 28

``````l <- list(l1, l2)
uni = list(union(l[][], l[][]))
uni = list(union(l[][], l[][]))
uni = list(union(l[][], l[][]))
``````

Eventually, I will have 100 lists in each set. I can do the task one by one, but it's not an efficient way to do it 100 times for 100 lists. Can anyone help me to do it efficiently, possibly using for loop? Thanks!

``````uni1 = list(union(l[][], l[][]))
uni2 = list(union(l[][], l[][]))
uni3 = list(union(l[][], l[][]))
``````

these can be replaced by

``````
uni <- purrr::map2(
l[],
l[],
union)``````

I feel that I sent you off into the wilderness of the brackets with an earlier answer, so let's circle back.

1. The uni s don't do what you are looking for
2. They can be made to, but it gets unnecessarily complicated
3. There's a simpler way.
4. The whole process can be simplified by reconsidering lists

So 1-3

``````l1 <- list(c(1, 2, 5), c(1,5), c(1, 2, 5))
l2 <- list(c(1,  4,  5, 16, 31, 40, 50), c(1,  4,  5, 39), c(1,  2,  5, 28, 34, 40, 45, 49))
l <- list(l1,l2)

uni1 = list(union(l[][], l[][]))
uni2 = list(union(l[][], l[][]))
uni3 = list(union(l[][], l[][]))
uni1;uni2;uni3
#> []
#>   1  2  5  4 16 31 40 50
#> []
#>   1  5  4 39
#> []
#>   1  2  5 28 34 40 45 49

a <- l[][]
b <- l[][]
a;b
#>  1 2 5
#>   1  4  5 16 31 40 50

a <- c(1,2,5)
b <- c(1,4,5,16,31,40,50)
d <- union(a,b)
setdiff(a,setdiff(d,a))
#>  1 2 5

a <- c(1, 2, 5)
b <- c(1, 4, 5, 16, 31, 40, 50)
unique(a, b)
#>  1 2 5
``````

Created on 2023-07-03 with reprex v2.0.2

How wed to lists?

Thanks a lot! It's really helpful.

Thanks a lot. I got the idea now.

1 Like

Hi, could you please tell me how to do it when I have three sets of lists? Thanks!

``````l1 <- list(c(1, 2, 5), c(1,5), c(1, 2, 5))
l2 <- list(c(1,  4,  5, 16, 31, 40, 50), c(1,  4,  5, 39), c(1,  2,  5, 28, 34, 40, 45, 49))
l3 <- list(c( 1 , 2,  4,  5,  9, 13, 15, 16, 19, 22, 25, 26, 28, 30, 31, 34, 40, 44),c(1,  2,  3,  4,  5,  6, 10, 11, 16, 20, 22, 27, 40, 43, 44), c(1,  2,  4,  5,  6,  9, 10, 11, 19, 20, 24, 25, 26, 28, 29, 33, 35, 40, 42, 44, 48, 49, 50))
``````

The following code works for only two sets of lists.

``````uni <- purrr::map2(
l[],
l[],
union)
``````

Hi, could you please tell me how to do it when I have three sets of lists? Thanks!

``````l1 <- list(c(1, 2, 5), c(1,5), c(1, 2, 5))
l2 <- list(c(1,  4,  5, 16, 31, 40, 50), c(1,  4,  5, 39), c(1,  2,  5, 28, 34, 40, 45, 49))
l3 <- list(c( 1 , 2,  4,  5,  9, 13, 15, 16, 19, 22, 25, 26, 28, 30, 31, 34, 40, 44),c(1,  2,  3,  4,  5,  6, 10, 11, 16, 20, 22, 27, 40, 43, 44), c(1,  2,  4,  5,  6,  9, 10, 11, 19, 20, 24, 25, 26, 28, 29, 33, 35, 40, 42, 44, 48, 49, 50))
``````
``````l1 <- list(c(1, 2, 5), c(1,5), c(1, 2, 5))
l2 <- list(c(1,  4,  5, 16, 31, 40, 50), c(1,  4,  5, 39), c(1,  2,  5, 28, 34, 40, 45, 49))
l3 <- list(c( 1 , 2,  4,  5,  9, 13, 15, 16, 19, 22, 25, 26, 28, 30, 31, 34, 40, 44),c(1,  2,  3,  4,  5,  6, 10, 11, 16, 20, 22, 27, 40, 43, 44), c(1,  2,  4,  5,  6,  9, 10, 11, 19, 20, 24, 25, 26, 28, 29, 33, 35, 40, 42, 44, 48, 49, 50))
l <- list(l1,l2,l3)
a <- l[][]
b <- l[][]
d <- l[][]
a;b;d
#>  1 2 5
#>   1  4  5 16 31 40 50
#>    1  2  4  5  9 13 15 16 19 22 25 26 28 30 31 34 40 44
unique(a,b,d)
#>  1 2 5
``````

Hi, Thanks for your try, but this is not what I want. I just want both common and uncommon elements from each list of l1, l2, and l3.

``````l1 <- list(c(1, 2, 5), c(1,2,4,5), c(1, 2,4, 5))
l2 <- list(c(1, 44, 50), c(1,  4), c(2,  4,  5,  6, 16, 21, 29, 31, 35))
l3 <- list(c(1,  4,  5, 28, 30, 34, 40), c(1,  2,  4,  5, 27, 43), c(1,  2,  4,  5, 11, 29, 33, 35, 44, 48, 49, 50))
``````

The output should look like
 1 2 5 28 30 34 40 44 50
 1 2 4 5 27 43
 1 2 4 5 6 11 16 21 29 31 33 35 44 48 49 50

We're back to where we started on the first thread, except this one is called "finding the union of two (now three) sets." "Union," "intersection" and difference refer to Boolean sets. If the word is meant as a non-mathematical description, then more detail is needed on the process by which the output shown is derived from the three lists.

1 Like
``````l1 <- list(c(1, 2, 5), c(1,5), c(1, 2, 5))
l2 <- list(c(1,  4,  5, 16, 31, 40, 50), c(1,  4,  5, 39), c(1,  2,  5, 28, 34, 40, 45, 49))
l3 <- list(c( 1 , 2,  4,  5,  9, 13, 15, 16, 19, 22, 25, 26, 28, 30, 31, 34, 40, 44),c(1,  2,  3,  4,  5,  6, 10, 11, 16, 20, 22, 27, 40, 43, 44), c(1,  2,  4,  5,  6,  9, 10, 11, 19, 20, 24, 25, 26, 28, 29, 33, 35, 40, 42, 44, 48, 49, 50))

l <- list(l1,l2,l3)

xunion <- function(...){
Reduce(f = union,
x = list(...))
}

(uni <- purrr::pmap(l,
xunion))``````
2 Likes

That provides what was asked for, but, sadly, not what was wanted.

apustat gave different lists to you the subsequent time than the first, confusing for sure; I dont think they've been particularly careful about stating their requirements, their thrust seems to be to want to union things though.

1 Like

The lists are different because they are generated from some computation associated with randomness; therefore, they should be different. My only concern is to get the union of the lists. As long as I know the procedure, I can do it for any lists. Thank you for your generous help.

In general in R to make reproducible randomness, use set.seed() function before the randomization happens. Hope this helps.

Yes, I know that, but that's not my intention. I want to check how my results vary because of randomness. Hope you got the whole scenario now.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.