[R] Generalized cumsum?

2009-09-16 Thread OKB (not okblacke)
Is there anything like cumsum and cumprod but which allows you to apply an arbitrary function instead of sum and product? In other words, I want a function cumfunc(x, f) that returns a vector, so that for all n up to the length of x cumapply(x,f)[n] = f(x[1:n]) This would give cumsum

Re: [R] Generalized cumsum?

2009-09-16 Thread baptiste auguie
?Reduce maybe HTH, baptiste 2009/9/16 OKB (not okblacke) brenb...@brenbarn.net Is there anything like cumsum and cumprod but which allows you to apply an arbitrary function instead of sum and product? In other words, I want a function cumfunc(x, f) that returns a vector, so that

Re: [R] Generalized cumsum?

2009-09-16 Thread OKB (not okblacke)
baptiste auguie wrote: ?Reduce maybe No, Reduce reduces an entire vector to a single value by repeatedly combining adjacent elements. I'm looking to convert a vector to another vector where each element is some arbitrary aggregating function applied to the first n elements of the

Re: [R] Generalized cumsum?

2009-09-16 Thread David Winsemius
On Sep 16, 2009, at 5:00 PM, OKB (not okblacke) wrote: Is there anything like cumsum and cumprod but which allows you to apply an arbitrary function instead of sum and product? In other words, I want a function cumfunc(x, f) that returns a vector, so that for all n up to the

Re: [R] Generalized cumsum?

2009-09-16 Thread David Winsemius
On Sep 16, 2009, at 5:09 PM, OKB (not okblacke) wrote: baptiste auguie wrote: ?Reduce maybe No, Reduce reduces an entire vector to a single value by repeatedly combining adjacent elements. I'm looking to convert a vector to another vector where each element is some arbitrary

Re: [R] Generalized cumsum?

2009-09-16 Thread Ted Harding
On 16-Sep-09 21:17:10, David Winsemius wrote: On Sep 16, 2009, at 5:09 PM, OKB (not okblacke) wrote: baptiste auguie wrote: ?Reduce maybe No, Reduce reduces an entire vector to a single value by repeatedly combining adjacent elements. I'm looking to convert a vector to another

Re: [R] Generalized cumsum?

2009-09-16 Thread OKB (not okblacke)
David Winsemius wrote: No, Reduce reduces an entire vector to a single value by repeatedly combining adjacent elements. I'm looking to convert a vector to another vector where each element is some arbitrary aggregating function applied to the first n elements of the

Re: [R] Generalized cumsum?

2009-09-16 Thread David Winsemius
On Sep 16, 2009, at 9:04 PM, OKB (not okblacke) wrote: David Winsemius wrote: No, Reduce reduces an entire vector to a single value by repeatedly combining adjacent elements. I'm looking to convert a vector to another vector where each element is some arbitrary aggregating function

Re: [R] Generalized cumsum?

2009-09-16 Thread OKB (not okblacke)
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. Best wishes, -- --OKB (not okblacke)

Re: [R] Generalized cumsum?

2009-09-16 Thread David Winsemius
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