【问题标题】:Keep Dates when aggregating xts object聚合 xts 对象时保留日期
【发布时间】:2016-01-13 09:53:31
【问题描述】:

我有一个 xts 对象,我想在其中聚合数据——同时保留日期。

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

我尝试使用索引来转换日期,但无济于事。

【问题讨论】:

  • 使用来自同一个 xts 包的 apply.weekly 函数。
  • 我尝试过使用它、to.period 和其他变体。 week = apply.weekly(dLn, FUN = index, dLn, as.POSIXlt(format(time(dLn)),tz="")) 如果在整个对象上执行它,当然日期聚合很好。
  • 我不明白你的评论,对不起。
  • 您需要提供reproducible example,因为不清楚您要做什么。你说你想聚合数据,但to.weekly 不聚合;它将 OHLC 或单变量系列转换为新的 OHLC 对象。您的数据似乎是返回的,因此创建 OHLC 对象没有意义。
  • 啊,谢谢!是的,它们是退货,那么有没有办法转换这些?

标签: r xts


【解决方案1】:

作为Pascal commented,您可以使用apply.weekly 来汇总您的回报(我在下面使用period.apply,因为它是一个通用解决方案)。您使用的聚合函数将取决于您的收益是如何计算的。

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)

【讨论】:

    猜你喜欢
    • 2014-04-12
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    相关资源
    最近更新 更多