按组

时间:2016-01-16 04:51:01

标签: r average dplyr plyr rollapply

基本上我想做最低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")

1 个答案:

答案 0 :(得分:0)

下面的代码为您提供了一些方法:对于您的100行data.frame,这将使用zoo :: rollapply()函数创建96个观测值的向量。目前还不清楚你想要对前四个观察做些什么,所以我暂时忽略了那个部分,但语法可能会被扩展,或者你可以写一个更详细的函数来传递给FUN。

mydata <- 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")
lowest_n <-  zoo::rollapply(mydata$value, width=5, FUN=function(x) mean(sort(x)[1:5], align=c('right')))