[R] How to calculate Rolling mean for a List object

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 15 01:07:04 CEST 2017


Try this code which does not use rollapply:

w <- 3
Mean <- function(L) Reduce("+", L) / length(L)
lapply(w:length(Data), function(i) Mean(Data[seq(to = i, length = w)]))

On Sun, May 14, 2017 at 6:44 PM, Christofer Bogaso
<bogaso.christofer at gmail.com> wrote:
> Hi again,
>
> I am looking to find a way on how to calculate Rolling average for the
> elements of a list. For example consider below object 'Data'. This is
> a list, where each elements are a Matrix. Basically, I am trying to
> get Rolling average of those Matrices with rolling window as 5.
>
> Data = structure(list(`2017-03-01` = structure(c(1.24915216491479e-06,
> -2.0209685810767e-06, -6.64165527006046e-06, -2.0209685810767e-06,
> 3.26966891657893e-06, 1.07453495291747e-05, -6.64165527006046e-06,
> 1.07453495291747e-05, 3.53132196103035e-05), .Dim = c(3L, 3L)),
>     `2017-03-02` = structure(c(0.00863066441403338, -7.25585852047094e-05,
>     -0.000950715788640005, -7.25585852047094e-05, 6.10004981580403e-07,
>     7.99273256915577e-06, -0.000950715788640005, 7.99273256915577e-06,
>     0.000104726642980084), .Dim = c(3L, 3L)), `2017-03-03` =
> structure(c(0.000785677680557358,
>     0.000283148300122928, 0.000170319078518317, 0.000283148300122928,
>     0.000102043066573597, 6.13808419844048e-05, 0.000170319078518317,
>     6.13808419844048e-05, 3.6921741860797e-05), .Dim = c(3L,
>     3L)), `2017-03-06` = structure(c(0.000100715163251975, 1.80035062425799e-06,
>     -5.05489732985851e-07, 1.80035062425799e-06, 3.21824665284709e-08,
>     -9.03596565752718e-09, -5.05489732985851e-07, -9.03596565752718e-09,
>     2.53705461922188e-09), .Dim = c(3L, 3L)), `2017-03-07` =
> structure(c(0.000640065014281149,
>     -0.000110994847091752, -0.000231235438845606, -0.000110994847091752,
>     1.92478198402357e-05, 4.00989612058198e-05, -0.000231235438845606,
>     4.00989612058198e-05, 8.35381203238728e-05), .Dim = c(3L,
>     3L)), `2017-03-08` = structure(c(7.72648041923266e-06,
> -2.11571338014623e-05,
>     7.82052544997182e-06, -2.11571338014623e-05, 5.79337921544145e-05,
>     -2.14146538093767e-05, 7.82052544997182e-06, -2.14146538093767e-05,
>     7.91571517626794e-06), .Dim = c(3L, 3L)), `2017-03-09` =
> structure(c(4.43321118550061e-05,
>     1.90242249279913e-05, 5.68672547310199e-05, 1.90242249279913e-05,
>     8.16385953582618e-06, 2.44034267661023e-05, 5.68672547310199e-05,
>     2.44034267661023e-05, 7.29467766214148e-05), .Dim = c(3L,
>     3L)), `2017-03-10` = structure(c(0.000100081081692311, 1.39245218598852e-05,
>     2.0935583168872e-05, 1.39245218598852e-05, 1.93735225227204e-06,
>     2.91281809264057e-06, 2.0935583168872e-05, 2.91281809264057e-06,
>     4.3794355057858e-06), .Dim = c(3L, 3L)), `2017-03-14` =
> structure(c(7.82185299651879e-06,
>     -3.05963602958646e-05, -4.65590052688468e-05, -3.05963602958646e-05,
>     0.00011968228804236, 0.000182122586662866, -4.65590052688468e-05,
>     0.000182122586662866, 0.000277139058045361), .Dim = c(3L,
>     3L)), `2017-03-15` = structure(c(4.02156693772954e-05, -2.2362610665311e-05,
>     -2.08706726432905e-05, -2.2362610665311e-05, 1.24351120722764e-05,
>     1.16054944222453e-05, -2.08706726432905e-05, 1.16054944222453e-05,
>     1.08312253240602e-05), .Dim = c(3L, 3L)), `2017-03-16` =
> structure(c(2.64254966198469e-05,
>     5.78730550194069e-06, 5.0445603894268e-05, 5.78730550194069e-06,
>     1.26744656702641e-06, 1.10478196556107e-05, 5.0445603894268e-05,
>     1.10478196556107e-05, 9.62993804379875e-05), .Dim = c(3L,
>     3L)), `2017-03-17` = structure(c(0.000138433807049962, 8.72005344938308e-05,
>     0.00014374477881467, 8.72005344938308e-05, 5.49282966209652e-05,
>     9.05459570205481e-05, 0.00014374477881467, 9.05459570205481e-05,
>     0.000149259504428865), .Dim = c(3L, 3L)), `2017-03-20` =
> structure(c(3.92058275846982e-05,
>     1.24332187386233e-05, -1.24235553811814e-05, 1.24332187386233e-05,
>     3.94290690251335e-06, -3.93984239286701e-06, -1.24235553811814e-05,
>     -3.93984239286701e-06, 3.93678026502162e-06), .Dim = c(3L,
>     3L)), `2017-03-21` = structure(c(0.000407544227952838,
> -6.22427018306449e-05,
>     1.90596071859105e-05, -6.22427018306449e-05, 9.50609446890975e-06,
>     -2.9109023406881e-06, 1.90596071859105e-05, -2.9109023406881e-06,
>     8.91360007491622e-07), .Dim = c(3L, 3L)), `2017-03-22` =
> structure(c(0.000220297355944482,
>     0.000282600064158173, 8.26030839524992e-05, 0.000282600064158173,
>     0.000362522718077154, 0.00010596421697645, 8.26030839524992e-05,
>     0.00010596421697645, 3.09729976068491e-05), .Dim = c(3L,
>     3L)), `2017-03-23` = structure(c(1.19559010537042e-05, 3.56054556562106e-05,
>     5.51130473489473e-06, 3.56054556562106e-05, 0.000106035376739222,
>     1.64130261253175e-05, 5.51130473489473e-06, 1.64130261253175e-05,
>     2.54054292892148e-06), .Dim = c(3L, 3L)), `2017-03-24` =
> structure(c(0.000573948692221572,
>     -7.36566239512158e-05, 5.40736580500709e-05, -7.36566239512158e-05,
>     9.45258404700116e-06, -6.93944101735685e-06, 5.40736580500709e-05,
>     -6.93944101735685e-06, 5.0944632064554e-06), .Dim = c(3L,
>     3L)), `2017-03-27` = structure(c(6.50931905856128e-06, -6.3937553506226e-07,
>     3.58314387213273e-06, -6.3937553506226e-07, 6.28024331206322e-08,
>     -3.51953024554351e-07, 3.58314387213273e-06, -3.51953024554351e-07,
>     1.97239064376729e-06), .Dim = c(3L, 3L)), `2017-03-28` =
> structure(c(3.59914960259327e-06,
>     -2.75070253522731e-05, -2.11089438966147e-05, -2.75070253522731e-05,
>     0.000210226449932902, 0.000161328179997165, -2.11089438966147e-05,
>     0.000161328179997165, 0.000123803554069931), .Dim = c(3L,
>     3L)), `2017-03-29` = structure(c(1.99662109539135e-05,
> -1.93486727571024e-05,
>     4.47577307389393e-05, -1.93486727571024e-05, 1.87502344999545e-05,
>     -4.33734115810541e-05, 4.47577307389393e-05, -4.33734115810541e-05,
>     0.0001003322295614), .Dim = c(3L, 3L)), `2017-03-30` =
> structure(c(2.23173776734009e-06,
>     4.62524439467561e-06, -2.71773191047607e-05, 4.62524439467561e-06,
>     9.58575242286433e-06, -5.63246025994455e-05, -2.71773191047607e-05,
>     -5.63246025994455e-05, 0.000330955851772093), .Dim = c(3L,
>     3L)), `2017-03-31` = structure(c(4.71973793042384e-05, 0.000129971028444871,
>     0.000195018983782496, 0.000129971028444871, 0.000357911148543379,
>     0.000537038671683375, 0.000195018983782496, 0.000537038671683375,
>     0.000805816013435771), .Dim = c(3L, 3L))), .Names = c("2017-03-01",
> "2017-03-02", "2017-03-03", "2017-03-06", "2017-03-07", "2017-03-08",
> "2017-03-09", "2017-03-10", "2017-03-14", "2017-03-15", "2017-03-16",
> "2017-03-17", "2017-03-20", "2017-03-21", "2017-03-22", "2017-03-23",
> "2017-03-24", "2017-03-27", "2017-03-28", "2017-03-29", "2017-03-30",
> "2017-03-31"))
>
> Normal rollmean() function works smoothly with a Zoo vector. However
> in my case, that function is not working, throwing below error :
>
>> rollmean(Data)
> Error in zoo(x) : “x” : attempt to define invalid zoo object
>
> Appreciate if someone points to some solution.
>
> Thanks for your time
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list