[Rd] Multiple Intersections

Martin Morgan mtmorgan at fhcrc.org
Wed May 28 16:58:30 CEST 2008


Hi Tony -- off-list, as I think you're looking for something else...

"Tony Chiang" <tchiang at fhcrc.org> writes:

> Hi all,
>
> I don't know if this is the correct venue for this question, but I am sure
> that someone will correct me if I am in the wrong list.
>
> I have been searching throughout R for a function that can find the
> intersection of multiple sets of "things". Say for instance, I have a list
> of $n$ character vectors and would like to find the intersection of all $k$
> subsets. I don't believe that there is such a function to do this (or am I
> wrong?). It is a pretty easy to encode such a function...there was an e-mail
> about how a recursive function to intersect an arbitrary number of sets
> which is elegant and useful (sorry I forgot the person's name who wrote the
> 2 line function).

Maybe this is what you were thinking of?

Intersect=function(x, ...) {
    if (length(list(...))>1)
        Intersect(x, Intersect(...))
    else
        intersect(x, ...)
}

> Intersect(letters[1:5], letters[3:6], letters[4:7])
[1] "d" "e"
> l=list(letters[1:5], letters[3:6], letters[4:7])
> do.call("Intersect", l)
[1] "d" "e"

Also

> Reduce(intersect, l, letters)
[1] "d" "e"

This won't be efficient for a large set of 'things', and I'm not
getting how n and k fit in -- 'all k subsets' implies all elements of
n?

Martin

> My question is two-fold:
>
> 1. If such a function already exists, what is it called?
> 2. If such a function does not exists, it is worthwhile to encode it (i.e.
> can I send my code to someone?).
>
> Cheers,
> --tony
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-devel mailing list