每n小时计算一次移动平均线

时间:2015-11-05 19:32:55

标签: r data.table

我有一个数据框,每个网站每小时有4次观察的时间序列。

我想计算每4个小时的移动平均线,使用data.table,我可以每小时计算,但不是每n小时。

dput(df)
structure(list(time = structure(c(1414502100, 1414503000, 1414503900, 
1414504800, 1414505700, 1414506600, 1414507500, 1414508400, 1414509300, 
1414510200, 1414511100, 1414512000, 1414512900, 1414513800, 1414514700, 
1414515600, 1414516500, 1414517400, 1414518300, 1414519200, 1414520100, 
1414521000, 1414521900, 1414522800, 1414523700, 1414524600, 1414525500, 
1414526400, 1414527300, 1414528200, 1414529100, 1414530000, 1414530900, 
1414531800, 1414532700, 1414533600, 1414534500, 1414535400, 1414536300, 
1414537200), class = c("POSIXct", "POSIXt"), tzone = ""), site = c(2108L, 
2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 
2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 
2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 
2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 2108L, 
2108L, 2108L, 2108L), val = c(38L, 38L, 35L, 35L, 35L, 35L, 37L, 
38L, 38L, 36L, 36L, 35L, 33L, 31L, 27L, 26L, 20L, 16L, 14L, 11L, 
7L, 5L, 2L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 1L, 0L, 0L), month = c(10, 10, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10), 
    hour = c("14", "14", "14", "15", "15", "15", "15", "16", 
    "16", "16", "16", "17", "17", "17", "17", "18", "18", "18", 
    "18", "19", "19", "19", "19", "20", "20", "20", "20", "21", 
    "21", "21", "21", "22", "22", "22", "22", "23", "23", "23", 
    "23", "00"), min = c("15", "30", "45", "00", "15", "30", 
    "45", "00", "15", "30", "45", "00", "15", "30", "45", "00", 
    "15", "30", "45", "00", "15", "30", "45", "00", "15", "30", 
    "45", "00", "15", "30", "45", "00", "15", "30", "45", "00", 
    "15", "30", "45", "00"), day = c(28L, 28L, 28L, 28L, 28L, 
    28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 
    28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 
    28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 29L)), .Names = c("time", 
"site", "val", "month", "hour", "min", "day"), class = "data.frame", row.names = 191430:191469)



 dt<- data.table(df)
    dt[, ':=' ('hsd' = sd(val)), by = list(site, hour, day)]

 head(dt, 10)
                   time site val month hour min day      hsd
 1: 2014-10-28 14:15:00 2108  38    10   14  15  28 1.732051
 2: 2014-10-28 14:30:00 2108  38    10   14  30  28 1.732051
 3: 2014-10-28 14:45:00 2108  35    10   14  45  28 1.732051
 4: 2014-10-28 15:00:00 2108  35    10   15  00  28 1.000000
 5: 2014-10-28 15:15:00 2108  35    10   15  15  28 1.000000
 6: 2014-10-28 15:30:00 2108  35    10   15  30  28 1.000000
 7: 2014-10-28 15:45:00 2108  37    10   15  45  28 1.000000

这是计算移动平均线的正确方法吗?如何计算移动平均线超过一小时

1 个答案:

答案 0 :(得分:2)

您可以使用dplyrzoo执行此操作。这里我按站点分组,即使您的示例数据只包含一个站点,因为我猜你的实际数据包括很多。我还假设你想要重叠间隔的值,而不是顺序的值。

library(zoo)
library(dplyr)

new.df <- df %>%
  group_by(site) %>%  # This only matters if your actual data have multiple sites
  mutate(moving.avg = rollmean(x = val, width = 16,  # 16 is 4 hours x 4 obs per hour
    align = "right", fill = NA))

如果您只想要连续窗口的平均值 - 即每四小时,或者在这种情况下每组中每16次观察一次 - 那么请rollapply使用by和{{1指定的选项,即:

align = "right"