Multiple unions

后端 未结 5 756
南笙
南笙 2021-01-04 08:00

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<         


        
相关标签:
5条回答
  • 2021-01-04 08:38

    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.

    0 讨论(0)
  • 2021-01-04 08:41
    unique(unlist(mget(mylists, globalenv())))
    

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

    0 讨论(0)
  • 2021-01-04 08:41

    ok this works but I am curious why sapply seems to have its own scope

    x<-sort(sample(1:20, 9))
    y<-sort(sample(10:30, 9))
    z<-sort(sample(20:40, 9))
    mylists<-c("x","y","z")
    emptyList<-vector()
    for(f in mylists){emptyList<-union(emptyList,get(f))}
    
    0 讨论(0)
  • 2021-01-04 08:48

    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

    0 讨论(0)
  • 2021-01-04 08:53
    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
    
    0 讨论(0)
提交回复
热议问题