[R] list matching

Gabor Grothendieck ggrothendieck at gmail.com
Sun Oct 7 02:18:02 CEST 2007


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.
>



More information about the R-help mailing list