I'd like to number each group in a data frame so that the groups are ordered according to the order they appear in the data frame. This is the code that I have so far:
library(tibble)
library(dplyr)
df <- tibble(
category = c("a", "b", "c", "c"),
value = c(7, 1, 4, 2)
)
df <- df %>%
group_by(category) %>%
mutate(mean_value = mean(value)) %>%
arrange(mean_value, category) %>%
ungroup()
df %>% mutate(id = group_indices(., category))
#> # A tibble: 4 x 4
#> category value mean_value id
#> <chr> <dbl> <dbl> <int>
#> 1 b 1.00 1.00 2
#> 2 c 4.00 3.00 3
#> 3 c 2.00 3.00 3
#> 4 a 7.00 7.00 1
I'd like the id variable to be ordered like this:
#> # A tibble: 4 x 4
#> category value mean_value id
#> <chr> <dbl> <dbl> <int>
#> 1 b 1.00 1.00 1
#> 2 c 4.00 3.00 2
#> 3 c 2.00 3.00 2
#> 4 a 7.00 7.00 3
I ordered the data frame according to the criteria that I wanted to use (mean_value), and now I'd like to number the groups to align with category.
Why does the group_indices function order alphabetically by default? Is there a simple way for me to achieve my goal?