xtsApply: An apply method for xts objects.

Description Usage Arguments Value Examples

View source: R/xtsApply.R

Description

This function implements an apply method for xts objects. I works in exactly the same was as apply does, but makes sure you get back an xts object. In some cases the standard apply mehtods return strange resutls when applied to xts objects. This function handles the operations correctly and will do column-wise apply when "margin" is set to 2, and row-wise when "margin" is set to 1. "cFUN" fills the roll of the the standard FUN in apply methods.

Usage

1
xtsApply(x, cFUN, margin = 2, ...)

Arguments

x

an xts object over which you want to apply some function.

cFUN

the function you wish to apply.

margin

controls whether you want column-wise apply (margin = 2) or row-wise apply (margin = 1). Defaults to margin = 2.

Value

an xts object where cFUN has been applied to the columns or rows.

Examples

1
2
3
4
5
6
dts <- seq(Sys.Date()-4, Sys.Date(), 1)
returns <- matrix(rnorm(10),ncol = 2) / 100
ret.xts <- xts(returns, dts)
xtsApply(ret.xts, cFUN = sum, margin = 1)

Note that apply(ret.xts,2,sum) will return a vector, NOT and xts object!

gtog/dMisc documentation built on May 17, 2019, 8:57 a.m.