Multiple unions

r
13,274

Solution 1

A not necessarily memory efficient paradigm that will work with GRanges is

Reduce(union, list(x, y, z))

The argument might also be a GRangesList(x, y, z) for appropriate values of x etc.

Solution 2

x<-sort(sample(1:20, 9))
y<-sort(sample(10:30, 9))
z<-sort(sample(20:40, 9))

Both of the below produce the same output

unique(c(x,y,z))
[1]  1  2  4  6  7  8 11 15 17 14 16 18 21 23 26 28 29 20 22 25 31 32 35

union(x,union(y,z))
[1]  1  2  4  6  7  8 11 15 17 14 16 18 21 23 26 28 29 20 22 25 31 32 35

Solution 3

I think it would be cleaner to separate the "dereference" part from the n-ary union part, e.g.

dereflist <- function(l) lapply(a,get)
nunion <- function(l) Reduce(union,l)

But if you look at how union works, you'll see that you could also do

nunion <- function(l) unique(do.call(c,l))

which is faster in all the cases I've tested (much faster for long lists).

-s

Solution 4

unique(unlist(mget(mylists, globalenv())))

will do the trick. (Possibly changing the environment given in the call to mget, as required.)

Solution 5

This can be done by using the reduce function in the purrr package.

purrr::reduce(list(x, y, z),union)

Share:
13,274

Related videos on Youtube

Jeremy Leipzig
Author by

Jeremy Leipzig

I am a bioinformatics software developer at Cytovas and PhD student at Drexel. You're more likely to find me at biostars.org than official Stack Exchange sites.

Updated on May 27, 2022

Comments

  • Jeremy Leipzig
    Jeremy Leipzig over 1 year

    I am trying to do unions on several lists (these are actually GRanges objects not integer lists but the priciple is the same), basically one big union.

    x<-sort(sample(1:20, 9))
    y<-sort(sample(10:30, 9))
    z<-sort(sample(20:40, 9))
    mylists<-c("x","y","z")
    emptyList<-list()
    sapply(mylists,FUN=function(x){emptyList<-union(emptyList,get(x))})
    

    That is just returning the list contents. I need the equivalent of

    union(x,union(y,z))
    [1]  2  3  5  6  7 10 13 15 20 14 19 21 24 27 28 29 26 31 36 39
    

    but written in an extensible and non-"variable explicit" form

  • Richie Cotton
    Richie Cotton almost 13 years
    If you look at the code for union, you'll see that it just calls unique on a pair of vector inputs.
  • Richie Cotton
    Richie Cotton almost 13 years
    In the question, function(x){emptyList<-union(emptyList,get(x))} assigns a local variable called emptyList. It does not overwrite the global value, which is why the code does not work.
  • Gavin Simpson
    Gavin Simpson almost 13 years
    Can you provide a full example of how you would call each of your functions to affect a solution, given the original objects from the Q. Otherwise, what is the point?
  • Stavros Macrakis
    Stavros Macrakis over 12 years
    Really? I'd like to think the original poster can figure out that nunion(dereflist(...)) will do it.
  • Gavin Simpson
    Gavin Simpson over 12 years
    It was the nesting of dereflist() inside nunion() that wasn't clear when I wrote the comment.
  • MadmanLee
    MadmanLee almost 6 years
    i'v found this won't work on large lists. Uses c instead of union.