Reputation: 4008
I've this data frame:
year Mes Sesiones
1 2014 Abril 25721
2 2014 Agosto 14851
3 2014 Diciembre 71359
4 2014 Enero 2244
5 2014 Febrero 3149
6 2014 Julio 11540
7 2014 Junio 18648
8 2014 Marzo 10738
9 2014 Mayo 19151
10 2014 Noviembre 149655
11 2014 Octubre 42032
12 2014 Setiembre 33667
13 2015 Enero 41885
14 2015 Febrero 81120
I've applied this to make the colum "Mes" (that originally was a character column), a factor column (With ordered levels):
IkasaVisitas_Meses_Meses$Mes <- factor(IkasaVisitas_Meses_Meses$Mes,
levels = c("Enero", "Febrero", "Marzo", "Abril",
"Mayo", "Junio", "Julio", "Agosto",
"Setiembre", "Octubre", "Noviembre",
"Diciembre"),
ordered=TRUE)
But i still can get the months ordered as it should be: "Enero", "Febrero" ... "Diciembre". I know this is an easy taks, so if you could point me to a good reference would be glad.
To make this data frame i used this code (using dplyr): I meantion this cause, "group_by" has caused me some problems in other situations:
IkasaVisitas_Meses_Meses <- IkasaVisitas_Meses %>%
group_by(year,Mes) %>%
summarise (Sesiones = sum(Sesiones))
Upvotes: 0
Views: 1034
Reputation: 146120
Since, from your comment it sounds like it worked, here's the full answer:
Changing to a factor is a good idea (it doesn't have to be an ordered factor, that's more of a modeling convention).
Use arrange
to order rows, use ungroup()
to remove a grouping:
IkasaVisitas_Meses_Meses <- IkasaVisitas_Meses %>%
group_by(year,Mes) %>%
summarise (Sesiones = sum(Sesiones)) %>%
mutate(Mes = factor(Mes,
levels = c("Enero", "Febrero", "Marzo", "Abril",
"Mayo", "Junio", "Julio", "Agosto",
"Setiembre", "Octubre", "Noviembre",
"Diciembre")) %>%
# if you don't want the re-rordering to be within-group
ungroup() %>%
arrange(Mes)
Upvotes: 1