Arrange Months Chronologically

226 views
Skip to first unread message

Dan Mungai

unread,
Jun 20, 2020, 4:15:09 AM6/20/20
to manipulatr
using dplyr to sort my data. however it is not arranging in months chronologically but alphabetically

Brandon Hurr

unread,
Jun 20, 2020, 11:43:57 AM6/20/20
to Dan Mungai, manipulatr
One of the easiest ways would be to convert your month column to an ordered factor and then arrange it. Or, if you only want to arrange it and not convert to a factor you could arrange it on the factor version of itself. See the answer on this SO post for a way to do it:
df %>%
    arrange(factor(Reg, levels = LETTERS[c(3, 1, 2)]), desc(Res), desc(Pop))

You could use the built-in month.name and month.abb variables if your spellings match those values. Assuming your column is called month and they are the full month names something like this should work.
df %>%
arrange(factor(month, levels = month.name))

On Sat, Jun 20, 2020 at 1:15 AM Dan Mungai <mungai...@gmail.com> wrote:
using dplyr to sort my data. however it is not arranging in months chronologically but alphabetically

--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/manipulatr/ef067c29-d994-434f-b133-e99edf102d22o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages