Thank you for the dput, but you roll the dice each time you fail to learn
how to post using plain text that we may not find your email readable or
even intact. Please learn to post plain text email on this mailing list.
# The following assumes you have no missing days in your time sequence.
# There are better solutions in packages if that is not true for your data
# (e.g. zoo or data.table)
# The filter function looks backward, while your example appears
# to look foward from the "current" value. The filter function
presumes
# that near the beginning the "previous" values are missing, which
causes
# it to generate NA values, which I remove and tack on the NAs at the end.
# (It would be misleading to put in alternate calculations at the end
# because those would be different than all the rest.)
dt$`Moving Average` <- c( filter( dt$Amount
, rep(1/3,3)
, sides = 1
)[ -(1:2) ]
, NA
, NA
)
dta <- aggregate( list( Max3dMA = dt$`Moving Average` )
, dt[ , "Year", drop=FALSE ]
, FUN = max
, na.rm = TRUE
)
On Sat, 22 Jul 2017, roslinazairimah zakaria wrote:
> Dear r-users,
>
> I would like to construct 3-day moving average for block maxima series.
>
> I tried this:
>
> bmthree <- lapply(split(dt, dt$Year), function(x)
max(sapply(1:(nrow(x)-2),
> function(i) with(x, mean(Amount[i:(i+2)],na.rm=TRUE)))))
> bmthree
>
> and got the following output.
>
> $`1971`
> [1] 70.81667
>
> $`1972`
> [1] 68.94553
>
> $`1973`
> [1] 102.7236
>
> $`1974`
> [1] 73.6625
>
> $`1975`
> [1] 92.98889
>
> $`1976`
> [1] 95.8125
>
> $`1977`
> [1] 31.33974
>
> $`1978`
> [1] 141
>
> $`1979`
> [1] 71.4
>
> $`1980`
> [1] 115.9667
>
> $`1981`
> [1] 66.73718
>
> $`1982`
> [1] 189.5
>
> $`1983`
> [1] 183.1
>
> $`1984`
> [1] 131.5667
>
> $`1985`
> [1] 96.83333
>
> $`1986`
> [1] 267.9667
>
> $`1987`
> [1] 113.6667
>
> $`1988`
> [1] 246.6667
>
> $`1989`
> [1] 83.33333
>
> $`1990`
> [1] 79.5
>
> $`1991`
> [1] 138.3333
>
> $`1992`
> [1] 117
>
> $`1993`
> [1] 99.66667
>
> $`1994`
> [1] 205.3333
>
> $`1995`
> [1] 142.6667
>
> $`1996`
> [1] 106.6667
>
> $`1997`
> [1] 112.5
>
> $`1998`
> [1] 62.66667
>
> $`1999`
> [1] 100.6333
>
> $`2000`
> [1] 146.1333
>
> $`2001`
> [1] 171.7333
>
> $`2002`
> [1] 78.33333
>
> $`2003`
> [1] 100.5667
>
> $`2004`
> [1] 115.0667
>
> $`2005`
> [1] 163.8667
>
> $`2006`
> [1] 79.93333
>
> $`2007`
> [1] 130.6667
>
> $`2008`
> [1] 156.5
>
> $`2009`
> [1] 162.1667
>
> $`2010`
> [1] 99.33333
> $`2011`
> [1] 162.8333
>
> $`2012`
> [1] 247.6667
>
> How do I tweak the code so that I can see the moving average for the block
> maxima before the maximum for that particular year is chosen.
>
> Year Month Day Amount
> Moving average 3-day BM
> 1971 1 1 55.81429 47.63658
> 1971 1 2 49.01818 34.95606
> 1971 1 3 38.07727
> 1971 1 4 17.77273
>
> This is my data:
>
>> dput(try.dt)
> structure(list(Year = c(1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L,
> 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L, 1971L), Month = c(1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
> 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
> 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
> 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
> 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
> 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
> 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
> 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
> 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
> 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
> 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
> 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
> 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
> 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
> 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
> 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
> 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L,
> 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
> 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
> 12L), Day = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
> 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
> 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
> 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
> 22L, 23L, 24L, 25L, 26L, 27L, 28L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
> 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
> 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L,
> 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
> 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
> 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,
> 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
> 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
> 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
> 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L,
> 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
> 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L,
> 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
> 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
> 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
> 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
> 25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
> 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
> 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L,
> 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L,
> 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,
> 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
> 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L,
> 28L, 29L, 30L, 31L), Amount = c(55.81428571, 49.01818182, 38.07727273,
> 17.77272727, 6.913636364, 5.163636364, 2.995454545, 4.540909091,
> 3.09047619, 3.959090909, 4.080952381, 0.513636364, 0.727272727,
> 0.940909091, 0.036363636, 0.672727273, 0.338095238, 1.163636364,
> 1.745454545, 1.004545455, 0.386363636, 0.359090909, 1.463636364,
> 0.640909091, 0.054545455, 3.768181818, 0.027272727, 0.027272727,
> 1.904545455, 8.261904762, 12.21363636, 12.48181818, 0.059090909,
> 2.468181818, 0.659090909, 0.157142857, 0.6, 0.477272727, 0.818181818,
> 0.05, 0.118181818, 0.527272727, 0.331818182, 0.004545455, 1.9,
> 2.018181818, 0.795454545, 2.118181818, 0.345454545, 0.045454545,
> 6.481818182, 13.45454545, 21.48636364, 23.98181818, 3.877272727,
> 3.945454545, 2.423809524, 0.672727273, 4.565, 11.625, 25.74761905,
> 14.51428571, 2.471428571, 0.085714286, 1.523809524, 0.652380952,
> 1.085714286, 0.485714286, 0.576190476, 0.1, 0.038095238, 1.361904762,
> 38.17619048, 102.1761905, 54.28095238, 20.2, 17.28095238, 25.53809524,
> 7.419047619, 0.576190476, 0.061904762, 0.061904762, 1.533333333,
> 0.028571429, 0.023809524, 0.057142857, 0.638095238, 0.70952381,
> 0.171428571, 0.014285714, 0.014285714, 0.557142857, 0.019047619,
> 1.090909091, 0.027272727, 0, 1.254545455, 0, 0.031818182, 0.418181818,
> 0.086363636, 0.186363636, 0.227272727, 0.009090909, 0.009090909,
> 0.981818182, 0.4, 0, 0, 0, 0, 0, 0, 0, 1.55, 0.327272727, 0.009090909,
> 1.413636364, 5.023809524, 11.11, 12.68181818, 12.63636364, 5.877272727,
> 1.018181818, 2.495454545, 3.25, 3.527272727, 1.177272727, 0.6,
> 1.836363636, 8.45, 6.745454545, 7.677272727, 1.177272727, 7.961904762,
> 6.786363636, 5.763636364, 4.047619048, 3.961904762, 7.933333333,
> 3.233333333, 1.114285714, 2.033333333, 6.257142857, 6.386363636,
> 2.327272727, 16.09090909, 13.87727273, 4.277272727, 1.036363636,
> 1.454545455, 1.086363636, 6.495454545, 4.136363636, 2.663636364,
> 10.49545455, 6.422727273, 2.881818182, 6.695454545, 1.631818182,
> 0.277272727, 3.686363636, 2.718181818, 1.281818182, 4.381818182,
> 0.759090909, 7.236363636, 8.195454545, 3.572727273, 3.395454545,
> 1.072727273, 3.827272727, 2.486363636, 0.481818182, 1.163636364,
> 1.5, 2.731818182, 2.568181818, 11.87727273, 12.60909091, 3.747826087,
> 2.204347826, 2.634782609, 1, 2.582608696, 1.213043478, 0.043478261,
> 0.930434783, 3.443478261, 0.743478261, 2.252173913, 2.117391304,
> 7.2, 9.730434783, 1.047826087, 0.213043478, 4.613043478, 6.77826087,
> 7.97826087, 19.52173913, 14.10434783, 3.652173913, 1.039130435,
> 3.873913043, 5.543478261, 2.717391304, 4.295652174, 5.417391304,
> 6.234782609, 7.556521739, 2.356521739, 7.413043478, 6.647826087,
> 5.139130435, 8.856521739, 8.4, 4.113043478, 10.20454545, 8.443478261,
> 3.886956522, 7.856521739, 6.047826087, 5.154166667, 4.241666667,
> 1.041666667, 0.820833333, 3.220833333, 1.0375, 4.545833333, 2.825,
> 4.769565217, 11.84782609, 5.773913043, 10.46521739, 7.77826087,
> 1.134782609, 1.17826087, 4.504347826, 7.247826087, 9.956521739,
> 13.77826087, 9.265217391, 0.534782609, 1.217391304, 1.72173913,
> 0.904347826, 0.426086957, 5.965217391, 2.191304348, 1.569565217,
> 7.360869565, 1.17826087, 6.417391304, 10.84782609, 9.508695652,
> 8.691304348, 15.5, 2.447826087, 2.739130435, 5.082608696, 10.53913043,
> 11.5826087, 2.143478261, 4.67826087, 4.969565217, 1.030434783,
> 3.073913043, 3.77826087, 0.952173913, 2.952173913, 3.669565217,
> 2.282608696, 2.740909091, 8.3, 3.67826087, 6.456521739, 8.952173913,
> 1.469565217, 2.147826087, 10.14782609, 9.008695652, 10.81304348,
> 6.460869565, 11.65652174, 9.195652174, 7.973913043, 15.65652174,
> 18.86956522, 6.930434783, 11.56086957, 12.99545455, 13.13913043,
> 19.23043478, 8.995652174, 5.556521739, 27.07391304, 20.94347826,
> 4.826086957, 5.586956522, 4.126086957, 7.1, 9.404545455, 6.47826087,
> 6.426086957, 12.35217391, 8.652173913, 4.752173913, 4.926086957,
> 2.304347826, 7.930434783, 9.782608696, 1.831818182, 1.877272727,
> 3.19047619, 9.940909091, 4.222727273, 21, 16.38636364, 10.56363636,
> 8.422727273, 7.533333333, 47.19090909, 101.9181818, 63.34090909,
> 24.98181818, 12.72272727, 7.4, 4.672727273, 8.104545455, 8.4,
> 7.473913043, 3.226086957, 15.87391304, 7.834782609, 6.773913043,
> 15.12608696, 36.89047619, 65.66521739, 53.70434783, 42.76956522,
> 48.49130435, 29.59130435, 28.59130435, 22.25652174, 15.08695652,
> 26.23913043, 21.17391304, 30.31818182, 28.59130435, 9.817391304,
> 14.02608696, 6.708695652, 11.74782609, 17.5375, 25.8826087, 12.55217391,
> 13.10869565, 5.02173913, 11.50869565, 5.195652174, 3.409090909,
> 0.340909091, 6.231818182, 0.004545455, 0.03)), .Names = c("Year",
> "Month", "Day", "Amount"), row.names =
8402:8766, class = "data.frame")
>
>
> Thank you very much for your help.
> --
> *Roslinazairimah Zakaria*
> *Tel: +609-5492370 <+60%209-549%202370>; Fax. No.+609-5492766
> <+60%209-549%202766>*
>
> *Email: roslinazairimah at ump.edu.my
> <roslinazairimah at ump.edu.my>; roslinaump at gmail.com
<roslinaump at gmail.com>*
> Faculty of Industrial Sciences & Technology
> University Malaysia Pahang
> Lebuhraya Tun Razak, 26300 Gambang, Pahang, Malaysia
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k