Dear list, Given a list of elements like: aa <- list(one=c("o", "n", "e"), tea=c("t", "e", "a"), thre=c("t", "h", "r", "e")) Is there a function that returns the intersection between all? Both match() and intersect() only deal with two arguments, but sometimes I have more. Here, it should return "e" (or NA if none of the letters are common). I have a solution to apply %in% multiple times (here two times, first between the first two and then between the result and the third) but... perhaps there is a better and quicker way. Thanks in advance, Adrian -- Adrian Dusa Romanian Social Data Archive 1, Schitu Magureanu Bd 050025 Bucharest sector 5 Romania Tel./Fax: +40 21 3126618 \ +40 21 3120210 / int.101
On Sat, 2007-10-06 at 19:49 +0000, Adrian Dusa wrote:> Dear list, > > Given a list of elements like: > aa <- list(one=c("o", "n", "e"), > tea=c("t", "e", "a"), > thre=c("t", "h", "r", "e")) > > Is there a function that returns the intersection between all? > Both match() and intersect() only deal with two arguments, but sometimes I > have more. > > Here, it should return "e" (or NA if none of the letters are common). > > I have a solution to apply %in% multiple times (here two times, first between > the first two and then between the result and the third) but... perhaps there > is a better and quicker way. > > Thanks in advance, > AdrianintersectList <- function(x) { res <- table(unlist(sapply(x, unique))) names(res[res == length(x)]) } In the first line, I use unique() to ensure that if the same letter appears more than once in the same list element, it is not included in the result set in error, such as in 'L' here: L <- list(a = c("a", "b", "b"), b = c("d", "b", "a"), c = c("d", "a"))> L$a [1] "a" "b" "b" $b [1] "d" "b" "a" $c [1] "d" "a" So:> intersectList(aa)[1] "e"> intersectList(L)[1] "a" HTH, Marc Schwartz
zoo's merge can do a multiway intersection. We turn each component of aa into the times of a dataless zoo object (assuming the elements of each component are unique) and merge them together using all = FALSE which will only leave those points at times in all components. Extracting the time and stripping off the names gives the result. library(zoo) as.vector(time(do.call(merge, c(lapply(aa, function(x) zoo(,x)), all = FALSE)))) On 10/6/07, Adrian Dusa <dusa.adrian at gmail.com> wrote:> > Dear list, > > Given a list of elements like: > aa <- list(one=c("o", "n", "e"), > tea=c("t", "e", "a"), > thre=c("t", "h", "r", "e")) > > Is there a function that returns the intersection between all? > Both match() and intersect() only deal with two arguments, but sometimes I > have more. > > Here, it should return "e" (or NA if none of the letters are common). > > I have a solution to apply %in% multiple times (here two times, first between > the first two and then between the result and the third) but... perhaps there > is a better and quicker way. > > Thanks in advance, > Adrian > > -- > Adrian Dusa > Romanian Social Data Archive > 1, Schitu Magureanu Bd > 050025 Bucharest sector 5 > Romania > Tel./Fax: +40 21 3126618 \ > +40 21 3120210 / int.101 > > ______________________________________________ > R-help at r-project.org 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. >
On 10/7/07, Adrian Dusa <dusa.adrian at gmail.com> wrote:> On Sunday 07 October 2007, Gabor Grothendieck wrote: > > I thought the latest zoo fixes were on CRAN but perhaps they > > are not. Try this: > > > > library(zoo) > > # next line loads latest version of merge.zoo (fixed in August) > > source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/ > >pkg/R/merge.zoo.R?rev=361&root=zoo") > > > > as.vector(time(do.call(merge, c(lapply(aa, function(x) zoo(,x)), all > > FALSE)))) > > Yeap, that fixes it. > Ah-haa, so all = FALSE is merge()s argument... > Neat :) > AdrianIt also makes use of the fact that zoo is sufficiently general that the times in the time series can be character strings. For example, the following time series has the value 1 at time "a", 2 at time "b", etc. zoo(1:26, letters)