0

How to calculate the time-weighted average of an xts object for e.g 5 minutes: I know weighted.mean() but don't know how to deal with the date and time. Thanks

 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))
Herr Student
  • 853
  • 14
  • 26
  • 1
    This isn't much better than the question you deleted. It's nice that you provided some sample data, but what is the output supposed to be? Your data are regular, so the regular mean and the time-weighted mean will be the same. It's still very unclear what you're asking. – Joshua Ulrich Jul 02 '13 at 19:45
  • Sorry I missed to delete one item in between, now it's not regular anymore - it's just example sometimes 5 values are missing in 1 hour. – Herr Student Jul 02 '13 at 19:53

1 Answers1

2

I'm still not sure what you're asking, but the code below will calculate the time-weighted mean in each non-overlapping 10-minute interval. All rows (except the first) will be the same as a regular mean because your data occur once every minute, except for between the 7th and 8th observations.

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
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418