[R] Generalized cumsum?

David Winsemius dwinsemius at comcast.net
Thu Sep 17 04:11:01 CEST 2009


On Sep 16, 2009, at 9:31 PM, OKB (not okblacke) wrote:

> David Winsemius wrote:
>
>> cumapply <- function (FUN, X)
>> { FUN <- match.fun(FUN)
>>   answer <- sapply(1:length(X), function(x) { FUN(X[1:x])} )
>>     return(answer)}
>
>   	Cool, thanks.  It's still interesting to me that there's no such
> thing built-in.

An admission; I just looked at the code for sapply and stripped it down
to essentials. I had already tested the logic with:

sapply(1:10, function(x) mean(vec[1:x])  )
###  [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5

If it turns out you want some of the extra features of sapply, the code
is right there for the taking. (It's actually a wrapper for lapply.)  
Type:

sapply

-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list