[R] list matching

Adrian Dusa dusa.adrian at gmail.com
Sat Oct 6 22:28:08 CEST 2007


On Saturday 06 October 2007, Marc Schwartz wrote:
> [...]
>
> intersectList <- 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"


Neat :o)

Of course, this was the right way to follow... in my particular case the 
elements are always unique but generally this is the best approach indeed.

Thanks Marc,
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



More information about the R-help mailing list