[R] Generalized cumsum?

OKB (not okblacke) brenbarn at brenbarn.net
Wed Sep 16 23:00:41 CEST 2009


    	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 and cumprod as special cases when f=sum or 
f=prod.

    	I could write such a function, but I can't see a way to do it 
without a loop, so I'm wondering if such a function exists that may be 
speedier.

Thanks,
-- 
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is
no path, and leave a trail."
	--author unknown




More information about the R-help mailing list