1

I have an xts object where I want to aggregate the data--while keeping the 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 to convert the dates, to no avail.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
BorgmanC
  • 11
  • 2
  • 1
    Use the `apply.weekly` function from the same `xts` package. –  Oct 15 '15 at 04:48
  • I have tried using that, to.period, and other variants. weekly = apply.weekly(dLn, FUN = index, dLn, as.POSIXlt(format(time(dLn)),tz="")) If performing it on the entire object, of course the Dates aggregate fine. – BorgmanC Oct 15 '15 at 05:04
  • I don't understand your comment, sorry. –  Oct 15 '15 at 05:05
  • You need to provide a [reproducible example](http://stackoverflow.com/q/5963269/271616) because it's not clear what you're trying to do. You say you want to aggregate the data, but `to.weekly` doesn't aggregate; it converts an OHLC or univariate series to a new OHLC object. Your data appear to be returns, so creating an OHLC object doesn't make sense. – Joshua Ulrich Oct 15 '15 at 18:16
  • Ah, thank you! Yes, they are returns, so is there a way to convert these? – BorgmanC Oct 16 '15 at 19:26

1 Answers1

0

As Pascal commented, you can use apply.weekly to aggregate your returns (I use period.apply below, since it is a general solution). The aggregation function you use will depend on how your returns are 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)
Community
  • 1
  • 1
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418