I have this df:
city | code |
---|---|
PR | 41 |
SC | 42 |
RS | 43 |
I want to change city to factor, but keep the order as in the code. If I only do
df %>% mutate(city= factor(city))
it orders alphabetically.
I would like something such as
df %>% mutate(city= factor(city, levels = code))
Thanks!
EDIT: I don't want to do manually levels = c('PR', 'SC', 'RS')
because it has many more categories!