【问题标题】:Find the mean of multiple observations in an extensible time series在可扩展的时间序列中查找多个观测值的平均值
【发布时间】:2020-03-20 04:08:16
【问题描述】:

我有一个 xts 在特定日期有多个表演:

                    Performance
2004-05-31        -7.478589e-03
2004-06-30         1.565250e-02
2004-06-30         1.372764e-02
2004-07-30        -1.558922e-03
2004-07-30        -1.451943e-02
2004-07-30        -3.829991e-02
2004-08-31        -4.456728e-03
2004-08-31        -1.547637e-03
2004-08-31         1.901513e-02

我想得到一个新的时间序列或数据框,没关系,用每个日期索引的手段:

                    Performance
2004-05-31        -7.478589e-03
2004-06-30         1.469007e-02 (mean of both 2004-06-30 observations)
2004-07-30        -5.225589e-03 (mean of three 2004-07-30 observations)
... 

我查看了 xts 备忘单和互联网,没有发现任何类似的东西。有人知道我可以使用什么功能吗?非常感谢您的宝贵时间。

【问题讨论】:

    标签: r xts


    【解决方案1】:

    一种选择是在tapply 中按index 分组并获得mean

    tapply(xt1, index(xt1), FUN = mean)
    #  2004-05-31   2004-06-30   2004-07-30   2004-08-31 
    #-0.007478589  0.014690070 -0.018126087  0.004336922 
    

    或者apply.daily

    library(xts)
    apply.daily(xt1, mean)
    #              [,1]
    #2004-05-31 -0.007478589
    #2004-06-30  0.014690070
    #2004-07-30 -0.018126087
    #2004-08-31  0.004336922
    

    数据

    xt1 <- structure(c(-0.007478589, 0.0156525, 0.01372764, -0.001558922, 
    -0.01451943, -0.03829991, -0.004456728, -0.001547637, 0.01901513
    ), .Dim = c(9L, 1L), index = structure(c(1085961600, 1088553600, 
    1088553600, 1091145600, 1091145600, 1091145600, 1093910400, 1093910400, 
    1093910400), tzone = "UTC", tclass = "Date"), class = c("xts", 
    "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC")
    

    【讨论】:

      猜你喜欢
      • 2018-07-12
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 2021-11-03
      • 2018-07-12
      相关资源
      最近更新 更多