0

如何计算 xts 对象的时间加权平均值,例如 5 分钟:我知道 weighted.mean() 但不知道如何处理日期和时间。谢谢

 library(xts)
    structure(c(28.2, 28.2, 28.2, 28.2, 28.1, 28.1, 28.1, 28.1, 28.1, 
28.2, 28.3, 28.2, 28.2, 28.1, 28.1, 28.1, 28.1, 28.1, 28, 28, 
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 27.9, 27.9, 27.9, 27.9, 
27.9, 27.9, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 
27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 
27.7, 27.7, 27.7, 27.8), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", 
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", index = structure(c(1356998400, 
1356998460, 1356998520, 1356998580, 1356998640, 1356998700, 1356998760, 
1356998880, 1356998940, 1356999000, 1356999060, 1356999120, 1356999180, 
1356999240, 1356999300, 1356999360, 1356999420, 1356999480, 1356999540, 
1356999600, 1356999660, 1356999720, 1356999780, 1356999840, 1356999900, 
1356999960, 1357000020, 1357000080, 1357000140, 1357000200, 1357000260, 
1357000320, 1357000380, 1357000440, 1357000500, 1357000560, 1357000620, 
1357000680, 1357000740, 1357000800, 1357000860, 1357000920, 1357000980, 
1357001040, 1357001100, 1357001160, 1357001220, 1357001280, 1357001340, 
1357001400, 1357001460, 1357001520, 1357001580, 1357001640, 1357001700, 
1357001760, 1357001820, 1357001880, 1357001940, 1357002000), tzone = "", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(60L, 1L))
4

1 回答 1

2

我仍然不确定你在问什么,但下面的代码将计算每个非重叠 10 分钟间隔中的时间加权平均值。所有行(第一行除外)都将与常规平均值相同,因为您的数据每分钟出现一次,第 7 和第 8 次观察之间除外。

x <- structure(c(28.2, 28.2, 28.2, 28.2, 28.1, 28.1, 28.1, 28.1, 28.1, 28.2,
  28.3, 28.2, 28.2, 28.1, 28.1, 28.1, 28.1, 28.1, 28, 28, 28, 28, 28, 28, 28,
  28, 28, 28, 28, 28, 27.9, 27.9, 27.9, 27.9, 27.9, 27.9, 27.8, 27.8, 27.8,
  27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8, 27.8,
  27.8, 27.8, 27.8, 27.8, 27.7, 27.7, 27.7, 27.8), class = c("xts", "zoo"),
  .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"),
  .indexTZ = "", tzone = "", index = structure(c(1356998400, 1356998460,
  1356998520, 1356998580, 1356998640, 1356998700, 1356998760, 1356998880,
  1356998940, 1356999000, 1356999060, 1356999120, 1356999180, 1356999240,
  1356999300, 1356999360, 1356999420, 1356999480, 1356999540, 1356999600,
  1356999660, 1356999720, 1356999780, 1356999840, 1356999900, 1356999960,
  1357000020, 1357000080, 1357000140, 1357000200, 1357000260, 1357000320,
  1357000380, 1357000440, 1357000500, 1357000560, 1357000620, 1357000680,
  1357000740, 1357000800, 1357000860, 1357000920, 1357000980, 1357001040,
  1357001100, 1357001160, 1357001220, 1357001280, 1357001340, 1357001400,
  1357001460, 1357001520, 1357001580, 1357001640, 1357001700, 1357001760,
  1357001820, 1357001880, 1357001940, 1357002000), tzone = "",
  tclass = c("POSIXct", "POSIXt")), .Dim = c(60L, 1L))
y <- cbind(x, c(0,diff(.index(x))))
f <- function(z) weighted.mean(z[,1],z[,2])
period.apply(y, endpoints(y, "minutes", 10), f)
#                         [,1]
# 2012-12-31 18:09:00 28.13333
# 2012-12-31 18:19:00 28.14000
# 2012-12-31 18:29:00 28.00000
# 2012-12-31 18:39:00 27.88000
# 2012-12-31 18:49:00 27.80000
# 2012-12-31 18:59:00 27.77000
# 2012-12-31 19:00:00 27.80000
于 2013-07-02T20:03:57.760 回答