I have a list of vectors in R and I would like to group all the vectors that share at least one value and than keep only the unique values. Do you know how I can code it in R, please?
For example, lets say that the list is the following:
[[1]]
[1] 1 2
[[2]]
[1] 2 1 3 7
[[3]]
[1] 3 2 4
[[4]]
[1] 4 3
[[5]]
[1] 17 10
[[6]]
[1] 17 22 10
[[7]]
[1] 22 17
I would like to obtain the following two vectors
(1,2,3,4,7)
(10,17,22)
I was thinking of using a loop where iteratively searching and adding elements that share at least one value with intersect and then applying unique. However, as my real list contains more than 50000 elements I was wondering if something more efficient exist, please.
Data
l <- list(c(1,2),c(2,1,3,7),c(3,2,4),c(4,3),c(17,10),c(17,22,10),c(22,17))