Hi, i have a list of several vectors, for example:> vectorlist$vector.a.1 [1] "a" "b" "c" $vector.a.2 [1] "a" "b" "d" $vector.b.1 [1] "e" "f" "g" I can use intersect to find elements that appear in $vector.a.1 and $vector.a.2:> intersect(vectorlist[[1]], vectorlist[[2]])[1] "a" "b" I would like to use grep to get the vectors by their names matching an expression and to find the intersects between those vectors. For the first step:> vectorlist[grep ("vector.a", names(vectorlist))]$vector.a.1 [1] "a" "b" "c" $vector.a.2 [1] "a" "b" "d" Unfortunately, I can not pass the two vectors as argument to intersect:> intersect(vectorlist[grep ("vector.a", names(vectorlist))])Error in unique(y[match(x, y, 0)]) : argument "y" is missing, with no default I am running R Version 2.3.1 (2006-06-01) Could somone help me to solve this? Cheers, Georg
FAQ 7.21. But there are perhaps slicker ways. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Georg Otto > Sent: Friday, July 21, 2006 10:39 AM > To: r-help at stat.math.ethz.ch > Subject: [R] intersect of list elements > > > Hi, > > i have a list of several vectors, for example: > > > vectorlist > $vector.a.1 > [1] "a" "b" "c" > > $vector.a.2 > [1] "a" "b" "d" > > $vector.b.1 > [1] "e" "f" "g" > > > I can use intersect to find elements that appear in $vector.a.1 and > $vector.a.2: > > > intersect(vectorlist[[1]], vectorlist[[2]]) > [1] "a" "b" > > > I would like to use grep to get the vectors by their names matching an > expression and to find the intersects between those vectors. For the > first step: > > > vectorlist[grep ("vector.a", names(vectorlist))] > $vector.a.1 > [1] "a" "b" "c" > > $vector.a.2 > [1] "a" "b" "d" > > > Unfortunately, I can not pass the two vectors as argument to > intersect: > > > intersect(vectorlist[grep ("vector.a", names(vectorlist))]) > Error in unique(y[match(x, y, 0)]) : argument "y" is missing, > with no default > > I am running R Version 2.3.1 (2006-06-01) > > > Could somone help me to solve this? > > Cheers, > > Georg > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Georg Otto wrote:> Hi, > > i have a list of several vectors, for example: > > >>vectorlist > > $vector.a.1 > [1] "a" "b" "c" > > $vector.a.2 > [1] "a" "b" "d" > > $vector.b.1 > [1] "e" "f" "g" > > > I can use intersect to find elements that appear in $vector.a.1 and > $vector.a.2: > > >>intersect(vectorlist[[1]], vectorlist[[2]]) > > [1] "a" "b" > > > I would like to use grep to get the vectors by their names matching an > expression and to find the intersects between those vectors. For the > first step: > > >>vectorlist[grep ("vector.a", names(vectorlist))] > > $vector.a.1 > [1] "a" "b" "c" > > $vector.a.2 > [1] "a" "b" "d" > > > Unfortunately, I can not pass the two vectors as argument to intersect: > > >>intersect(vectorlist[grep ("vector.a", names(vectorlist))]) > > Error in unique(y[match(x, y, 0)]) : argument "y" is missing, with no default > > I am running R Version 2.3.1 (2006-06-01) > > > Could somone help me to solve this? > > Cheers, > > Georg > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.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
The following assumes that within each component of vectorlist the vector elements are unique. In that case the first two lines define vectorlist and perform the grep, as in your post. Elements of the intersection must occur n times where n is the number of components of vectorlist that match the grep and those elements are extracted in the last line. # from your post vectorlist <- list(vector.a.1 = c("a", "b", "c"), vector.a.2 = c("a", "b", "d"), vector.b.1. = c("e", "f", "g")) idx <- grep("vector.a", names(vectorlist)) # get intersection names(which(table(unlist(vectorlist[idx])) == length(idx))) On 7/21/06, Georg Otto <georg.otto at tuebingen.mpg.de> wrote:> > Hi, > > i have a list of several vectors, for example: > > > vectorlist > $vector.a.1 > [1] "a" "b" "c" > > $vector.a.2 > [1] "a" "b" "d" > > $vector.b.1 > [1] "e" "f" "g" > > > I can use intersect to find elements that appear in $vector.a.1 and > $vector.a.2: > > > intersect(vectorlist[[1]], vectorlist[[2]]) > [1] "a" "b" > > > I would like to use grep to get the vectors by their names matching an > expression and to find the intersects between those vectors. For the > first step: > > > vectorlist[grep ("vector.a", names(vectorlist))] > $vector.a.1 > [1] "a" "b" "c" > > $vector.a.2 > [1] "a" "b" "d" > > > Unfortunately, I can not pass the two vectors as argument to intersect: > > > intersect(vectorlist[grep ("vector.a", names(vectorlist))]) > Error in unique(y[match(x, y, 0)]) : argument "y" is missing, with no default > > I am running R Version 2.3.1 (2006-06-01) > > > Could somone help me to solve this? > > Cheers, > > Georg > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >