How to sort a data frame by date


R Problem Overview

I need to sort a data frame by date in R. The dates are all in the form of "dd/mm/yyyy". The dates are in the 3rd column. The column header is V3. I have seen how to sort a data frame by column and I have seen how to convert the string into a date value. I can't combine the two in order to sort the data frame by date.

R Solutions

Solution 1 - R

Assuming your data frame is named d,

d[order(as.Date(d$V3, format="%d/%m/%Y")),]

Read my blog post, Sorting a data frame by the contents of a column, if that doesn't make sense.

Solution 2 - R

Nowadays, it is the most efficient and comfortable to use lubridate and dplyr libraries.

lubridate contains a number of functions that make parsing dates into POSIXct or Date objects easy. Here we use dmy which automatically parses dates in Day, Month, Year formats. Once your data is in a date format, you can sort it with dplyr::arrange (or any other ordering function) as desired:

d$V3 <- lubridate::dmy(d$V3)
dplyr::arrange(d, V3)

Solution 3 - R

In case you want to sort dates with descending order the minus sign doesn't work with Dates.

out <- DF[rev(order(as.Date(DF$end))),]

However you can have the same effect with a general purpose function: rev(). Therefore, you mix rev and order like:

#init data
DF <- data.frame(ID=c('ID3', 'ID2','ID1'), end=c('4/1/09 12:00', '6/1/10 14:20', '1/1/11 11:10')
#change order
out <- DF[rev(order(as.Date(DF$end))),]

Hope it helped.

Solution 4 - R

You can use order() to sort date data.

# Sort date ascending order
d[order(as.Date(d$V3, format = "%d/%m/%Y")),]
# Sort date descending order
d[rev(order(as.Date(d$V3, format = "%d/%m/%y"))),]

Hope this helps,

Link to my quora answer


Solution 5 - R

If you just want to rearrange dates from oldest to newest in r etc. you can always do:

dataframe <- dataframe[nrow(dataframe):1,]

It's saved me exporting in and out from excel just for sort on Yahoo Finance data.

Solution 6 - R

The only way I found to work with hours, through an US format in source (mm-dd-yyyy HH-MM-SS PM/AM)...

df_dataSet$time <- as.POSIXct( df_dataSet$time , format = "%m/%d/%Y %I:%M:%S %p" , tz = "GMT")
df_dataSet <- df_dataSet[, df_dataSet), ] 

Solution 7 - R

You could also use arrange from the dplyr library.

The following snippet will modify your original date string to a date object, and order by it. This is a good approach, as you store a date as a date, not just a string of characters.

dates <- dates %>%
  mutate(date = as.Date(date, "%d/%m/%Y")) %>%

If you just want to order by the string (usually an inferior option), you can do this:

dates <- dates %>%
  arrange(date = as.Date(date, "%d/%m/%Y"))

Solution 8 - R

If you have a dataset named daily_data:

daily_data <- daily_data[order(as.Date(daily_data$date, format="%d/%m/%Y")),] 


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnView Question on Stackoverflow
Solution 1 - RI82MuchView Answer on Stackoverflow
Solution 2 - RLove-RView Answer on Stackoverflow
Solution 3 - RpommedeterresauteeView Answer on Stackoverflow
Solution 4 - RNitzView Answer on Stackoverflow
Solution 5 - RCameron TujenView Answer on Stackoverflow
Solution 6 - RAurélien PrévotView Answer on Stackoverflow
Solution 7 - RPrestonView Answer on Stackoverflow
Solution 8 - RSATYAJIT MAITRAView Answer on Stackoverflow