Learn R Programming

cgwtools (version 4.1)

cumfun: Function calculate the cumulative result of any function along an input vector.

Description

Calculates the cumulative value of almost any function in the same manner as cumsum

Usage

cumfun(frist, FUN = sum, ...)

Value

A list of the cumulative calculations generated.

Arguments

frist

A vector of numerical or string values

FUN

The function to use. Can be either the function name (as is usually done) or a single character string naming the function. Default is sum.

...

Additional arguments, if any, required for the chosen FUN function. See Details.

Author

Carl Witthoft, carl@witthoft.com

Details

If additional arguments are of length one, they are applied to each value in frist. If they are longer but not equal in length to length(frist) they will either be truncated or recycled to match. The builtin functions cumsum and cumprod generally are much faster than applying sum or prod to this function.

See Also

Examples

Run this code
foo <- 'abcdefcghicklmno'
grepfoo <-function(x,y) grep(y,x)
cumfoo <- cumfun(unlist(strsplit(foo,'')), FUN=grepfoo, y = 'c')
bar <- rnorm(1000,1)
cumsd <- cumfun(bar,FUN=sd)
plot(unlist(cumsd),type='l')
# compare with input std dev
lines(c(0,1000),c(1,1),lty=2,col='red')

Run the code above in your browser using DataLab