Re: [R] intersect more than two sets On 4/24/07, Weiwei Shi  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 ______________________________________________ R-help@stat.math.ethz.ch 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.