Hi, You may try: df <- structure(list(....) lapply(split(df, df$Year), function(x) sapply(1:(nrow(x)-2), function(i) with(x, mean(Amount[i:(i+2)],na.rm=TRUE)))) #$`1985` # [1] 4.1666667 3.8333333 0.8333333 0.3333333 0.3333333 0.0000000 0.0000000 # [8] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 #[15] 0.0000000 0.6666667 1.0000000 1.0000000
A.K. On Wednesday, June 18, 2014 10:53 PM, Hafizuddin Arshad <hafizuddinarsha...@gmail.com> wrote: Dear R users, I have this kind of data set: structure(list(Year = c(1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L), Day = 1:20, Month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Amount = c(1, 10, 1.5, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0)), .Names = c("Year", "Day", "Month", "Amount"), row.names = c(NA, 20L), class = "data.frame") I would like to do a block average for every three days i.e. Average(1Jan-3Jan, 2Jan-4Jan, 3Jan-5Jan,...29Dec-31Dec, 30Dec-1jan). I want to do this for each year and take the maximum value of the 3 day averages in each year to create a series of maxima. How to do this in R? Many thanks for helping me. Arshad [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list 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. ______________________________________________ R-help@r-project.org mailing list 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.