How to sort day and month columns chronologically in R

I have day and month columns in a dataframe which are of character type.But i am unable to sort them chronologically.

Data Examples:-

Day Month
Fri Jul
Tue Aug
Sat Apr
Tue May
Mon Jul
Fri Sep

Would you mind explaining what the objective of sorting the data is? Do you want the days or months to appear in chronological order in a plot perhaps?

I want both days and months to be chronologically displayed in various things such as aggregate functions and plots as well.

To handle the month information you could do something like:

Month <- factor(Month, levels = month.abb)

This will convert the Month column to a sorted factor. month.abb is a built in vector of abbreviated month names. For full month names, use month.name.

You could use a similar approach on the Day names, sorting according to your preference i.e. week starting on Sunday or Monday. I don't know of a built in vector of day name, but it's probably better to create your own ordering.

3 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.