Skip to content
Prev 96595 / 398500 Next

intersect of list elements

Georg Otto wrote:
Will this work for you?

vectorlist <- list(vector.a.1 = c("a", "b", "c"),
                    vector.a.2 = c("a", "b", "d"),
                    vector.b.1 = c("e", "f", "g"))

intersect2 <- function(...) {
   args <- list(...)
   nargs <- length(args)
   if(nargs <= 1) {
     if(nargs == 1 && is.list(args[[1]])) {
       do.call("intersect2", args[[1]])
     } else {
       stop("cannot evaluate intersection fewer than 2 arguments")
     }
   } else if(nargs == 2) {
     intersect(args[[1]], args[[2]])
   } else {
     intersect(args[[1]], intersect2(args[-1]))
   }
}

vector.a <- vectorlist[grep ("vector.a", names(vectorlist))]
intersect2(vector.a)
intersect2(vectorlist)

HTH,

--sundar