How to order the data based on date column

Hi everyone,

I want to order my dataset based on date column(particularly on month ) how can i do it?

my date column is in the format of yyyy-mm-dd i want to order the data only based on mm. please help

The month() function from the lubridate package will extract the month value from a date. If your data frame is named DF and the column is named Date, you can make a new column named month with

library(lubridate)
DF$month <- month(DF$Date)

That will work if the Date column is a numeric date. If the Date column is characters, it should work to do

DF$month <- month(as.Date(DF$Date))

Once you have the month column, you can sort on that.

1 Like

Thanks for the solution provided!

This topic was automatically closed 7 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.