r - Keep Dates when aggregating xts object -
i have xts object want aggregate data--while keeping dates.
wln = apply(dln, 2, to.weekly) > head(wln) aapl gspc vix [1,] -0.05602066 -0.0252409007 0.01903172 [2,] -0.03609222 -0.0111470571 0.13182832
i tried using index convert dates, no avail.
as pascal commented, can use apply.weekly
aggregate returns (i use period.apply
below, since general solution). aggregation function use depend on how returns calculated.
require(xts) set.seed(21) x <- xts(matrix(rnorm(60, 0, 0.01), ncol=2), sys.date()-30:1) # weekly endpoints wep <- endpoints(x, "weeks") # log returns period.apply(x, wep, colsums) # arithmetic returns period.apply(x, wep, function(x) apply(1+x,2,prod)-1)
Comments
Post a Comment