I want to get the mean value (amount) of the first 20%, second 20%, third 20% etc of each group within a data frame.
I have started a reprex below but don't know how to finish it. I think I will need to calculate the start and end of each 20% using a row number, which I've added as the ID is not sequential or predictable.
The actual data will have more than 2 groups and there will be more than one value to mean.
library(tidyverse)
df <- data.frame(id = c(0, 12, 23, 34, 45, 56, 67, 78, 89, 90, 9, 98, 87, 76, 65, 54, 43, 32, 21, 99),
group = c(1, 1, 2, 2, 2, 1, 1, 2, 2, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 2),
amount = c(102, 312, 567, 432, 345, 987, 94, 34, 54, 32, 92, 67, 32, 49, 36, 31, 19, 18, 17, 308))
groupSplitMeans <- df %>%
mutate(rowNumber = row_number()) %>%
group_by(group)