r - Average of last lowest N prior values by group -


basically want rolling average of lowest n values.

i have tried using

   mydata$lowest_n = rollapply(values, 5, mean(sort[1:5]), align=c('right')) 

but cannot work. again needs rolling time series data set. above code know has obvious error, don't have attempted methods in front of me. advice appreciated!!!!

if matters, have scenario many different groups, grouped using ddply()

my data =

  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") 

the code below gets part of way there: 100 row data.frame, creates vector of 96 observations using zoo::rollapply() function. it's not clear want first 4 observations, i'm ignoring part now, syntax expanded, or write more detailed function pass 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'))) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -