基本上我想做一个最低 N 值的滚动平均值。
我试过使用
mydata$lowest_n = rollapply(values, 5, mean(sort[1:5]), align=c('right'))
但我无法让它发挥作用。同样,这需要滚动,因为这是一个时间序列数据集。我知道上面的代码有一个明显的错误,只是我面前没有我尝试过的方法。任何建议表示赞赏!!!!
如果重要的话,我对许多不同的组都有这种情况,这些组已经使用 ddply() 进行了分组
我的数据 =
structure(list(Date = structure(c(13586, 13587, 13594, 13635,
13656, 13657, 13686, 13710, 13712, 13718, 13726, 13753, 13783,
13791, 13874, 13895, 13910, 13917, 13923, 13930, 13958, 13970,
13977, 13978, 13991, 14018, 14021, 14066, 14070, 14073, 14104,
14112, 14118, 14138, 14220, 14269, 14293, 14473, 14631, 13566,
13692, 13916, 14084, 12677, 12984, 13035, 13222, 13406, 13417,
13483, 13539, 13580, 13607, 13644, 13693, 13698, 13713, 13714,
13726, 13727, 13750, 13754, 13777, 13809, 13810, 13812, 13819,
13832, 13853, 13893, 13944, 13954, 14015, 14021, 14050, 14051,
14092, 14104, 14119, 14134, 14209, 14218, 14267, 14302, 14309,
14334, 14337, 14379, 14391, 14428, 14449, 14475, 14525, 14546,
14552, 14579, 14589, 12545, 12587, 12693), class = "Date"), value = c(15,
27, 15, 25, 16, 22, 27, 23, 16, 19, 22, 21, 15, 20, 22, 28, 22,
27, 20, 25, 28, 16, 16, 28, 24, 28, 22, 28, 22, 14, 28, 24, 16,
15, 28, 22, 28, 28, 27, 19, 20, 19, 24, 19, 25, 22, 24, 16, 28,
19, 18, 20, 20, 21, 19, 20, 22, 21, 20, 21, 23, 24, 17, 19, 28,
24, 30, 20, 20, 18, 21, 15, 16, 26, 19, 20, 19, 17, 20, 16, 18,
29, 21, 23, 18, 18, 26, 26, 25, 13, 13, 15, 18, 17, 20, 15, 18,
23, 29, 21)), .Names = c("Date", "value"), row.names = c(NA,
100L), class = "data.frame")