[R] intersect more than two sets

hadley wickham h.wickham at gmail.com
Tue Apr 24 21:25:56 CEST 2007


On 4/24/07, Weiwei Shi <helprhelp at gmail.com> wrote:
> assume t2 is a list of size 11 and each element is a vector of characters.
>
> the following codes can get what I wanted but I assume there might be
> a one-line code for that:
>
> t3 <- t2[[1]]
> for ( i in 2:11){
>         t3 <- intersect(t2[[i]], t3)
> }
>
> or there is no such "apply"?

The operation you want is called a fold
(http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29), and
if it was available in R, you'd be able to do:

fold(t2, intersect)

Unfortunately, it's not, but you could implement it as follows:

fold <- function(x, fun) {
	if (length(x) == 1) return(fun(x))
	
	accumulator <- fun(x[[1]], x[[2]])
	if (length(x) == 2) return(accumulator)

	for(i in 3:length(x)) {
		accumulator <- fun(accumulator, x[[i]])
	}
	accumulator
}

a <- list(c(1,3,5), c(1,3), c(1, 2, 5, 6))
fold(a, intersect)

Which is just a trivial generalisation of your code above

Hadley



More information about the R-help mailing list