period.apply: Apply Function Over Specified Interval

period.applyR Documentation

Apply Function Over Specified Interval

Description

Apply a specified function to data over intervals specified by INDEX. The intervals are defined as the observations from INDEX[k]+1 to INDEX[k+1], for k = 1:(length(INDEX)-1).

Usage

period.apply(x, INDEX, FUN, ...)

Arguments

x

The data that FUN will be applied to.

INDEX

A numeric vector of index breakpoint locations. The vector should begin with 0 and end with NROW(x).

FUN

A function to apply to each interval in x.

...

Additional arguments for FUN.

Details

Similar to the rest of the apply family, period.apply() calculates the specified function's value over a subset of data. The primary difference is that period.apply() applies the function to non-overlapping intervals of a vector or matrix.

Useful for applying functions over an entire data object by any non-overlapping intervals. For example, when INDEX is the result of a call to endpoints().

period.apply() checks that INDEX is sorted, unique, starts with 0, and ends with NROW(x). All those conditions are true of vectors returned by endpoints().

Value

An object with length(INDEX) - 1 observations (assuming INDEX starts with 0 and ends with NROW(x)).

Author(s)

Jeffrey A. Ryan, Joshua M. Ulrich

See Also

endpoints apply.monthly

Examples

zoo.data <- zoo(rnorm(31)+10,as.Date(13514:13744,origin="1970-01-01"))
ep <- endpoints(zoo.data,'weeks')
period.apply(zoo.data, INDEX=ep, FUN=function(x) mean(x))
period.apply(zoo.data, INDEX=ep, FUN=mean)                  #same

period.apply(letters,c(0,5,7,26), paste0)

xts documentation built on April 17, 2023, 1:07 a.m.