Intersection of lists in R

后端 未结 2 1459
挽巷
挽巷 2021-02-13 03:08

Is there a function that receives a list x and returns a list y such that y[[i]] = intersect(x[[1]][[i]], x[[2]][[i]], ...) ?

If n

2条回答
  •  一个人的身影
    2021-02-13 03:18

    Does this work?

    x <- list(list(1:3,2:4),list(2:3,4:5),list(3:7,4:5))
    maxlen <- max(sapply(x,length))
    lapply(seq(maxlen),function(i) Reduce(intersect,lapply(x,"[[",i)))
    

    (intersect only takes two arguments so you have to use Reduce as an additional step)

    PS I haven't tried this on any hard cases -- e.g. lists of uneven length.

提交回复
热议问题