Below is a dataframe (toy example) that I would like to transform so that group becomes 1, 1, 2, 2, 3, 3, 3.
group y
C -1.55461160
C 0.34945015
A 0.57210825
A -0.88019528
H 0.03307085
H 1.13494754
H -1.65146164
My current solution is to count the number of groups and the number of records per group, and to recreate the group variable using these two pieces, i.e.
ngroups <- length(unique(df$group))
npergroup <- aggregate(x = rep(1, nrow(df)), by = list(df$group), FUN = sum)$x
df <- df %>%
mutate(group = rep(1:ngroups, npergroup))
For the sake of elegance, do you have a fully dplyr solution?