I would like to find a dplyr way to take average for the next 3 rows. Say I have a data frame:
data <- structure(list(x = 1:6, y = c(32.1056789265246, 3.48493686329687, 8.21300282100191, 6.72266588891445, 27.7353607044612, 18.5963631547696)), .Names = c("x", "y"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L))
A tibble: 6 × 2
x y
<int> <dbl>
1 1 12.8230546
2 2 3.4083329
3 3 0.4825815
4 4 13.6714485
5 5 8.9829427
6 6 2.5997503
I want to generate a new data frame that has 3 rows with first one the average from row 2,3,4 and next from 3,4,5 and last one from 4,5,6.
A for loop is probably the easiest way but I would appreciate if there is some more elegant dplyr way to go...Thanks!