hadley wickham wrote:
> On 4/24/07, Weiwei Shi <[EMAIL PROTECTED]> 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)
>
>
It's come up before. Gabor G posted this rather more succinct version:
> Fold <- function(f, x, L) (for(e in L) x <- f(x, e))
> Fold(intersect,a[[1]],a[-1])
[1] 1
or maybe prettier:
> (E <- Fold(union, NULL, a))
[1] 1 3 5 2 6
> Fold(intersect, E, a)
[1] 1
> Which is just a trivial generalisation of your code above
>
______________________________________________
[email protected] 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.