3

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))
rawr
  • 20,481
  • 4
  • 44
  • 78
Helm
  • 39
  • 2
  • 3
    put your list into a data frame `dd <- data.frame(id = rep(seq_along(l), lengths(l)), l = unlist(l))` and your problem becomes [this](http://stackoverflow.com/questions/36659114/using-two-grouping-designations-to-create-one-combined-grouping-variable) – rawr May 13 '16 at 18:42
  • It worked. Thank you very much. – Helm May 27 '16 at 19:33
  • You can answer your own question with what you did, upvote, and accept – rawr May 27 '16 at 19:42

0 Answers0