Objective: calculate the mean of each pair of values by group in a dataframe
Example data:
mtcars[1:10, 1:2] %>%
arrange(cyl)
mpg cyl
22.8 4
24.4 4
22.8 4
21.0 6
21.0 6
21.4 6
18.1 6
19.2 6
18.7 8
14.3 8
Expected Output:
mpg cyl avg
22.8 4 -
24.4 4 23.6
22.8 4 23.6
21.0 6 -
21.0 6 21.0
21.4 6 21.2
18.1 6 19.75
19.2 6 18.65
18.7 8 -
14.3 8 16.5
Thought I could use dplyr::lag
or dplyr::slice
to do this, but haven't got it working. Not married to dplyr, any solution would be appreciated...
Attempt:
group_by(mtcars, gear) %>%
summarise(pairmean=mean(c(mpg, lag(mpg))))