# Combining two sets of lists using intersection

Hello,

I have two sets of lists, and they look like
list1:
[[1]]
[1] 1 2 5

[[2]]
[1] 1 5

[[3]]
[1] 1 2 5

list2
[[1]]
[1] "1" "2" "5"

[[2]]
[1] "1" "3" "5"

[[3]]
[1] "1" "2" "5" "28"

Now I want to get three final lists using intersection (both common and uncommon). How to do that in R? My desired outcome should look like
[[1]] 1 2 5
[[2]] 1 3 5
[[3]] 1 2 5 28

I can do it just for 3 lists, but eventually, I will have 2 sets of 100 lists and I need a final intersected (both common and uncommon) list. A for loop might be used. Please help me out. Thanks!

I can't follow the set logic. Why is list_1[2] excluded whlle list_2[3] is kept? They have in common that they are the only instance of their value in either list or in the union of both lists.

So, I'll reformulate the question and provide a solution:

f(x) = y where

x is a list, consisting of two lists, each of which consists of 100 lists of a variable number of vectors of varying length, and the first of the two lists is all numeric and the second of the two lists is all character

y is a list of three lists each of which contains a variable number of numeric vectors. The first list consists of all vectors of x[1] that are unique to \$x[1], the second list consists of all vectors that are unique to x[2] and the third list consists of all vectors that are common to x[1] and x[2].

f is a composite function, chain of functions or script to convert x to y

``````l1 <- list(
c(1, 7, 5), c(9, 9, 9, 3), c(2, 5),
c(
2,
6
), 3:4, c(8, 8, 3), c(9, 4, 4, 9), c(4, 7), c(
8,
7, 6, 2
), c(3, 8), c(1, 3), c(2, 8), c(8, 4), c(
9,
9, 7
), c(1, 5), c(5, 7), 7:6, c(8, 8, 6), c(
3, 9,
6, 8
), c(8, 9, 3), c(2, 9, 1, 2), 2:3, c(6, 7, 5),
c(2, 4), c(8, 4, 3), c(2, 6, 7, 9), 7:6, c(
9, 9,
8, 3
), c(5, 8, 1), 3:6, c(1, 5), c(8, 7, 2), c(
1,
3, 2
), c(2, 4, 5), c(9, 4), c(2, 4, 6), c(
9, 6,
9, 1
), c(2, 3, 2, 9), c(7, 8, 2), 6:7, c(
8, 4,
8
), c(9, 5, 3, 2), c(3, 2, 2, 4), c(6, 9), c(
4,
7, 2
), c(6, 9, 9), c(9, 6, 8, 8), c(
2, 5, 9,
4
), c(4, 9, 6, 9), c(2, 7), c(3, 9, 5, 6), 6:8,
7:6, c(8, 2, 4, 8), c(3, 6, 2), 3:2, c(8, 4, 6),
c(7, 9, 3), c(7, 7, 9), 8:9, c(6, 4, 8, 2), c(
7,
2, 9, 5
), c(3, 7, 5, 6), c(3, 9, 4, 6), c(
5,
7, 1
), c(2, 3, 7), c(1, 3), c(2, 4, 4), c(1, 6), c(4, 3, 5), c(5, 8), c(6, 8, 9), c(6, 4, 1),
c(4, 9, 7, 8), c(8, 2), c(1, 8, 5, 1), c(
3, 3,
8, 6
), 3:2, c(4, 4, 4, 2), c(9, 3, 8), c(
8, 3,
2
), c(6, 2, 8, 9), c(4, 4, 4), c(8, 8), c(
9, 3,
9
), c(4, 3, 1, 1), c(1, 9, 6, 5), c(7, 5), c(
2,
2, 3
), c(1, 1, 2), c(7, 3, 4), 7:6, c(
2, 9, 4,
7
), c(3, 3), c(2, 9, 2), c(9, 5, 6), c(1, 3),
c(7, 2), c(7, 3, 4), c(4, 1, 2)
)

l2 <- list(
c(
"1",
"5", "5"
), c("1", "1"), c("9", "9", "3", "8"), c("6", "8", "5"), c("2", "1"), c("2", "4", "4"), c("2", "8"), c("2", "7", "4"), c("1", "4", "7"), c("5", "3"), c("3", "2", "5"), c(
"9", "4",
"5", "2"
), c("7", "9", "6"), c("3", "3"), c("4", "8", "9"), c(
"8",
"7", "1"
), c("7", "2", "4", "4"), c("5", "2"), c("6", "4", "4"), c("5", "4", "9", "7"), c("1", "2", "2"), c("7", "3", "8"),
c("6", "4", "9"), c("8", "6", "3", "1"), c(
"3", "8", "1",
"9"
), c("3", "2"), c("3", "8"), c("2", "4"), c(
"7", "4",
"3"
), c("4", "3", "3"), c("1", "4", "2", "3"), c(
"8", "4",
"5", "9"
), c("3", "4", "3"), c("2", "9", "4"), c("4", "7"), c("4", "3"), c("6", "3"), c("3", "7", "9"), c(
"1", "9",
"2", "3"
), c("8", "6", "3", "4"), c("2", "9"), c(
"8", "4",
"3"
), c("9", "4"), c("1", "3", "2"), c("8", "9"), c(
"8",
"1", "2"
), c("6", "2"), c("8", "6", "7", "1"), c("7", "1"), c("6", "3"), c("7", "1", "1"), c("7", "2"), c(
"9", "6",
"3"
), c("7", "8"), c("8", "3"), c("9", "4"), c("8", "8"),
c("3", "7"), c("4", "9"), c("2", "4", "9", "3"), c("2", "6"), c("6", "9", "4"), c("9", "3"), c("5", "1", "4"), c(
"6",
"4"
), c("9", "1"), c("7", "9"), c("9", "2", "4", "7"), c(
"8",
"6", "6", "3"
), c("5", "4"), c("2", "9"), c("7", "5", "5"), c("9", "8", "7"), c("5", "5", "3", "3"), c("1", "8", "4"), c("9", "3", "2", "9"), c("1", "3"), c("2", "7", "7"),
c("2", "7", "2", "8"), c("9", "3"), c("8", "9", "6", "7"),
c("8", "7", "8"), c("6", "6", "6"), c("1", "7", "9"), c(
"6",
"7", "9"
), c("8", "6", "1"), c("5", "1", "4"), c(
"1", "6",
"9", "3"
), c("2", "8"), c("6", "1"), c("8", "9"), c(
"6",
"3", "4"
), c("6", "7", "2", "6"), c("2", "9", "4", "7"),
c("4", "8", "8", "2"), c("9", "2", "3"), c("6", "3", "5"),
c("7", "1", "6", "8"), c("5", "1"), c("4", "1", "2")
)

# convert to all numeric

l2 <- sapply(l2, as.numeric)
l <- list(l1, l2)
result <- list(
only_in_l1 = setdiff(l[1][[1]], l[2][[1]]),
only_in_l2 = setdiff(l[2][[1]], l[1][[1]]),
in_both    = intersect(l[1][[1]], l[2][[1]])
)
result[1]
#> \$only_in_l1
#> \$only_in_l1[[1]]
#> [1] 1 7 5
#>
#> \$only_in_l1[[2]]
#> [1] 9 9 9 3
#>
#> \$only_in_l1[[3]]
#> [1] 2 5
#>
#> \$only_in_l1[[4]]
#> [1] 3 4
#>
#> \$only_in_l1[[5]]
#> [1] 8 8 3
#>
#> \$only_in_l1[[6]]
#> [1] 9 4 4 9
#>
#> \$only_in_l1[[7]]
#> [1] 8 7 6 2
#>
#> \$only_in_l1[[8]]
#> [1] 8 4
#>
#> \$only_in_l1[[9]]
#> [1] 9 9 7
#>
#> \$only_in_l1[[10]]
#> [1] 1 5
#>
#> \$only_in_l1[[11]]
#> [1] 5 7
#>
#> \$only_in_l1[[12]]
#> [1] 7 6
#>
#> \$only_in_l1[[13]]
#> [1] 8 8 6
#>
#> \$only_in_l1[[14]]
#> [1] 3 9 6 8
#>
#> \$only_in_l1[[15]]
#> [1] 8 9 3
#>
#> \$only_in_l1[[16]]
#> [1] 2 9 1 2
#>
#> \$only_in_l1[[17]]
#> [1] 2 3
#>
#> \$only_in_l1[[18]]
#> [1] 6 7 5
#>
#> \$only_in_l1[[19]]
#> [1] 2 6 7 9
#>
#> \$only_in_l1[[20]]
#> [1] 9 9 8 3
#>
#> \$only_in_l1[[21]]
#> [1] 5 8 1
#>
#> \$only_in_l1[[22]]
#> [1] 3 4 5 6
#>
#> \$only_in_l1[[23]]
#> [1] 8 7 2
#>
#> \$only_in_l1[[24]]
#> [1] 2 4 5
#>
#> \$only_in_l1[[25]]
#> [1] 2 4 6
#>
#> \$only_in_l1[[26]]
#> [1] 9 6 9 1
#>
#> \$only_in_l1[[27]]
#> [1] 2 3 2 9
#>
#> \$only_in_l1[[28]]
#> [1] 7 8 2
#>
#> \$only_in_l1[[29]]
#> [1] 6 7
#>
#> \$only_in_l1[[30]]
#> [1] 8 4 8
#>
#> \$only_in_l1[[31]]
#> [1] 9 5 3 2
#>
#> \$only_in_l1[[32]]
#> [1] 3 2 2 4
#>
#> \$only_in_l1[[33]]
#> [1] 6 9
#>
#> \$only_in_l1[[34]]
#> [1] 4 7 2
#>
#> \$only_in_l1[[35]]
#> [1] 6 9 9
#>
#> \$only_in_l1[[36]]
#> [1] 9 6 8 8
#>
#> \$only_in_l1[[37]]
#> [1] 2 5 9 4
#>
#> \$only_in_l1[[38]]
#> [1] 4 9 6 9
#>
#> \$only_in_l1[[39]]
#> [1] 2 7
#>
#> \$only_in_l1[[40]]
#> [1] 3 9 5 6
#>
#> \$only_in_l1[[41]]
#> [1] 6 7 8
#>
#> \$only_in_l1[[42]]
#> [1] 8 2 4 8
#>
#> \$only_in_l1[[43]]
#> [1] 3 6 2
#>
#> \$only_in_l1[[44]]
#> [1] 3 2
#>
#> \$only_in_l1[[45]]
#> [1] 8 4 6
#>
#> \$only_in_l1[[46]]
#> [1] 7 9 3
#>
#> \$only_in_l1[[47]]
#> [1] 7 7 9
#>
#> \$only_in_l1[[48]]
#> [1] 8 9
#>
#> \$only_in_l1[[49]]
#> [1] 6 4 8 2
#>
#> \$only_in_l1[[50]]
#> [1] 7 2 9 5
#>
#> \$only_in_l1[[51]]
#> [1] 3 7 5 6
#>
#> \$only_in_l1[[52]]
#> [1] 3 9 4 6
#>
#> \$only_in_l1[[53]]
#> [1] 5 7 1
#>
#> \$only_in_l1[[54]]
#> [1] 2 3 7
#>
#> \$only_in_l1[[55]]
#> [1] 1 6
#>
#> \$only_in_l1[[56]]
#> [1] 4 3 5
#>
#> \$only_in_l1[[57]]
#> [1] 5 8
#>
#> \$only_in_l1[[58]]
#> [1] 6 8 9
#>
#> \$only_in_l1[[59]]
#> [1] 6 4 1
#>
#> \$only_in_l1[[60]]
#> [1] 4 9 7 8
#>
#> \$only_in_l1[[61]]
#> [1] 8 2
#>
#> \$only_in_l1[[62]]
#> [1] 1 8 5 1
#>
#> \$only_in_l1[[63]]
#> [1] 3 3 8 6
#>
#> \$only_in_l1[[64]]
#> [1] 4 4 4 2
#>
#> \$only_in_l1[[65]]
#> [1] 9 3 8
#>
#> \$only_in_l1[[66]]
#> [1] 8 3 2
#>
#> \$only_in_l1[[67]]
#> [1] 6 2 8 9
#>
#> \$only_in_l1[[68]]
#> [1] 4 4 4
#>
#> \$only_in_l1[[69]]
#> [1] 9 3 9
#>
#> \$only_in_l1[[70]]
#> [1] 4 3 1 1
#>
#> \$only_in_l1[[71]]
#> [1] 1 9 6 5
#>
#> \$only_in_l1[[72]]
#> [1] 7 5
#>
#> \$only_in_l1[[73]]
#> [1] 2 2 3
#>
#> \$only_in_l1[[74]]
#> [1] 1 1 2
#>
#> \$only_in_l1[[75]]
#> [1] 7 3 4
#>
#> \$only_in_l1[[76]]
#> [1] 2 9 2
#>
#> \$only_in_l1[[77]]
#> [1] 9 5 6
result[2]
#> \$only_in_l2
#> \$only_in_l2[[1]]
#> [1] 1 5 5
#>
#> \$only_in_l2[[2]]
#> [1] 1 1
#>
#> \$only_in_l2[[3]]
#> [1] 9 9 3 8
#>
#> \$only_in_l2[[4]]
#> [1] 6 8 5
#>
#> \$only_in_l2[[5]]
#> [1] 2 1
#>
#> \$only_in_l2[[6]]
#> [1] 2 7 4
#>
#> \$only_in_l2[[7]]
#> [1] 1 4 7
#>
#> \$only_in_l2[[8]]
#> [1] 5 3
#>
#> \$only_in_l2[[9]]
#> [1] 3 2 5
#>
#> \$only_in_l2[[10]]
#> [1] 9 4 5 2
#>
#> \$only_in_l2[[11]]
#> [1] 7 9 6
#>
#> \$only_in_l2[[12]]
#> [1] 4 8 9
#>
#> \$only_in_l2[[13]]
#> [1] 8 7 1
#>
#> \$only_in_l2[[14]]
#> [1] 7 2 4 4
#>
#> \$only_in_l2[[15]]
#> [1] 5 2
#>
#> \$only_in_l2[[16]]
#> [1] 6 4 4
#>
#> \$only_in_l2[[17]]
#> [1] 5 4 9 7
#>
#> \$only_in_l2[[18]]
#> [1] 1 2 2
#>
#> \$only_in_l2[[19]]
#> [1] 7 3 8
#>
#> \$only_in_l2[[20]]
#> [1] 6 4 9
#>
#> \$only_in_l2[[21]]
#> [1] 8 6 3 1
#>
#> \$only_in_l2[[22]]
#> [1] 3 8 1 9
#>
#> \$only_in_l2[[23]]
#> [1] 3 2
#>
#> \$only_in_l2[[24]]
#> [1] 7 4 3
#>
#> \$only_in_l2[[25]]
#> [1] 4 3 3
#>
#> \$only_in_l2[[26]]
#> [1] 1 4 2 3
#>
#> \$only_in_l2[[27]]
#> [1] 8 4 5 9
#>
#> \$only_in_l2[[28]]
#> [1] 3 4 3
#>
#> \$only_in_l2[[29]]
#> [1] 2 9 4
#>
#> \$only_in_l2[[30]]
#> [1] 4 3
#>
#> \$only_in_l2[[31]]
#> [1] 6 3
#>
#> \$only_in_l2[[32]]
#> [1] 3 7 9
#>
#> \$only_in_l2[[33]]
#> [1] 1 9 2 3
#>
#> \$only_in_l2[[34]]
#> [1] 8 6 3 4
#>
#> \$only_in_l2[[35]]
#> [1] 2 9
#>
#> \$only_in_l2[[36]]
#> [1] 8 9
#>
#> \$only_in_l2[[37]]
#> [1] 8 1 2
#>
#> \$only_in_l2[[38]]
#> [1] 6 2
#>
#> \$only_in_l2[[39]]
#> [1] 8 6 7 1
#>
#> \$only_in_l2[[40]]
#> [1] 7 1
#>
#> \$only_in_l2[[41]]
#> [1] 7 1 1
#>
#> \$only_in_l2[[42]]
#> [1] 9 6 3
#>
#> \$only_in_l2[[43]]
#> [1] 7 8
#>
#> \$only_in_l2[[44]]
#> [1] 8 3
#>
#> \$only_in_l2[[45]]
#> [1] 3 7
#>
#> \$only_in_l2[[46]]
#> [1] 4 9
#>
#> \$only_in_l2[[47]]
#> [1] 2 4 9 3
#>
#> \$only_in_l2[[48]]
#> [1] 6 9 4
#>
#> \$only_in_l2[[49]]
#> [1] 9 3
#>
#> \$only_in_l2[[50]]
#> [1] 5 1 4
#>
#> \$only_in_l2[[51]]
#> [1] 6 4
#>
#> \$only_in_l2[[52]]
#> [1] 9 1
#>
#> \$only_in_l2[[53]]
#> [1] 7 9
#>
#> \$only_in_l2[[54]]
#> [1] 9 2 4 7
#>
#> \$only_in_l2[[55]]
#> [1] 8 6 6 3
#>
#> \$only_in_l2[[56]]
#> [1] 5 4
#>
#> \$only_in_l2[[57]]
#> [1] 7 5 5
#>
#> \$only_in_l2[[58]]
#> [1] 9 8 7
#>
#> \$only_in_l2[[59]]
#> [1] 5 5 3 3
#>
#> \$only_in_l2[[60]]
#> [1] 1 8 4
#>
#> \$only_in_l2[[61]]
#> [1] 9 3 2 9
#>
#> \$only_in_l2[[62]]
#> [1] 2 7 7
#>
#> \$only_in_l2[[63]]
#> [1] 2 7 2 8
#>
#> \$only_in_l2[[64]]
#> [1] 8 9 6 7
#>
#> \$only_in_l2[[65]]
#> [1] 8 7 8
#>
#> \$only_in_l2[[66]]
#> [1] 6 6 6
#>
#> \$only_in_l2[[67]]
#> [1] 1 7 9
#>
#> \$only_in_l2[[68]]
#> [1] 6 7 9
#>
#> \$only_in_l2[[69]]
#> [1] 8 6 1
#>
#> \$only_in_l2[[70]]
#> [1] 1 6 9 3
#>
#> \$only_in_l2[[71]]
#> [1] 6 1
#>
#> \$only_in_l2[[72]]
#> [1] 6 3 4
#>
#> \$only_in_l2[[73]]
#> [1] 6 7 2 6
#>
#> \$only_in_l2[[74]]
#> [1] 4 8 8 2
#>
#> \$only_in_l2[[75]]
#> [1] 9 2 3
#>
#> \$only_in_l2[[76]]
#> [1] 6 3 5
#>
#> \$only_in_l2[[77]]
#> [1] 7 1 6 8
#>
#> \$only_in_l2[[78]]
#> [1] 5 1
result[3]
#> \$in_both
#> \$in_both[[1]]
#> [1] 2 6
#>
#> \$in_both[[2]]
#> [1] 4 7
#>
#> \$in_both[[3]]
#> [1] 3 8
#>
#> \$in_both[[4]]
#> [1] 1 3
#>
#> \$in_both[[5]]
#> [1] 2 8
#>
#> \$in_both[[6]]
#> [1] 2 4
#>
#> \$in_both[[7]]
#> [1] 8 4 3
#>
#> \$in_both[[8]]
#> [1] 1 3 2
#>
#> \$in_both[[9]]
#> [1] 9 4
#>
#> \$in_both[[10]]
#> [1] 2 4 4
#>
#> \$in_both[[11]]
#> [1] 8 8
#>
#> \$in_both[[12]]
#> [1] 2 9 4 7
#>
#> \$in_both[[13]]
#> [1] 3 3
#>
#> \$in_both[[14]]
#> [1] 7 2
#>
#> \$in_both[[15]]
#> [1] 4 1 2
``````

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

This topic was automatically closed 21 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.