rlistdata.tablelapplyset-operations

Union of intersecting vectors in a list in R


I have a list of vectors as follows.

data <- list(v1=c("a", "b", "c"), v2=c("g", "h", "k"), 
             v3=c("c", "d"), v4=c("n", "a"), v5=c("h", "i"))

I am trying to achieve the following:

  1. Check whether any of the vectors intersect with each other
  2. If intersecting vectors are found, get their union

So the desired output is

out <- list(v1=c("a", "b", "c", "d", "n"), v2=c("g", "h", "k", "i"))

I can get the union of a group of intersecting sets as follows.

 Reduce(union, list(data[[1]], data[[3]], data[[4]]))
 Reduce(union, list(data[[2]], data[[5]])

How to first identify the intersecting vectors? Is there a way of dividing the list into lists of groups of intersecting vectors?

#Update

Here is an attempt using data.table. Gets the desired results. But still slow for large lists as in this example dataset.

datasets. 
data <- sapply(data, function(x) paste(x, collapse=", "))
data <- as.data.frame(data, stringsAsFactors = F)

repeat {
  M <- nrow(data)
  data <- data.table( data , key = "data" )
  data <- data[ , list(dataelement = unique(unlist(strsplit(data , ", " )))), by = list(data)]
  data <- data.table(data , key = "dataelement" )
  data <- data[, list(data = paste0(sort(unique(unlist(strsplit(data, split=", ")))), collapse=", ")), by = "dataelement"]
  data$dataelement <- NULL
  data <- unique(data)
  N <- nrow(data)
  if (M == N)
    break
}

data <- strsplit(as.character(data$data) , "," )

Solution

  • This is kind of like a graph problem so I like to use the igraph library for this, using your sample data, you can do

    library(igraph)
    #build edgelist
    el <- do.call("rbind",lapply(data, embed, 2))
    #make a graph
    gg <- graph.edgelist(el, directed=F)
    #partition the graph into disjoint sets
    split(V(gg)$name, clusters(gg)$membership)
    
    # $`1`
    # [1] "b" "a" "c" "d" "n"
    # 
    # $`2`
    # [1] "h" "g" "k" "i"
    

    And we can view the results with

    V(gg)$color=c("green","purple")[clusters(gg)$membership]
    plot(gg)
    

    enter image description here