Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple unions

Tags:

r

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

like image 710
Jeremy Leipzig Avatar asked Apr 01 '11 16:04

Jeremy Leipzig


2 Answers

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.

like image 83
Martin Morgan Avatar answered Oct 11 '22 16:10

Martin Morgan


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
like image 43
agent18 Avatar answered Oct 11 '22 15:10

agent18