cumfun {cgwtools}R Documentation

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, ...)

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.

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.

Value

A list of the cumulative calculations generated.

Author(s)

Carl Witthoft, carl@witthoft.com

See Also

cumsum , cumall, rowCumsums

Examples

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')

[Package cgwtools version 4.1 Index]