简体   繁体   English

在可扩展的时间序列中查找多个观测值的平均值

[英]Find the mean of multiple observations in an extensible time series

I have an xts with multiple performances on a specific date:我有一个在特定日期有多个表演的 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

I would like to get a new time series or dataframe, does not really matter, with the means of every date index:我想获得一个新的时间序列或数据框,这并不重要,使用每个日期索引的方法:

                    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)
... 

I have looked through the xts cheat sheat and the interwebs and have found nothing similar.我浏览了 xts 作弊表和互联网,没有发现类似的东西。 Does anybody have an idea what function i could use?有人知道我可以使用什么功能吗? Thank you very much for your time.非常感谢您的宝贵时间。

One option is to group by the index in tapply and get the mean一种选择是按tapplyindex分组并获得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 

Or with apply.daily或者使用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

data数据

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM