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.
8 Answers
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.

- 26,901
- 13
- 88
- 119
-
@John how about voting up the answer? :) – Prasad Chalasani Jun 05 '11 at 22:50
-
@Prasad Chalasani I'd be glad to, if I knew how – John Jun 05 '11 at 22:59
-
@John there is an up-arrow on the left of the answer. You need to click it. And if you feel it is the best of several answers, you can also click the check mark indicating "Accepted answer". – Prasad Chalasani Jun 05 '11 at 23:03
-
@Prasad Chalasani Sorry it turns out I don't have enough points to be able to vote – John Jun 05 '11 at 23:06
-
@John -- ah yes, I forgot about that! – Prasad Chalasani Jun 05 '11 at 23:15
-
@John if this is the correct answer, please check the tick next to my suggestion so I get the accepted answer credit. – I82Much Jun 06 '11 at 01:51
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)
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.

- 10,931
- 9
- 38
- 47

- 1,843
- 1
- 20
- 24
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 https://qr.ae/TWngCe
Thanks

- 81
- 1
- 4
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.

- 29
- 1
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")
class(df_dataSet$time)
df_dataSet <- df_dataSet[do.call(order, df_dataSet), ]

- 21
- 1
-
Thanks! I was struggling to sort with posix-formatted dates, and this is the only solution that worked. – jls Aug 06 '18 at 12:54
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")) %>%
arrange(date)
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"))

- 7,399
- 8
- 54
- 84
If you have a dataset named daily_data
:
daily_data <- daily_data[order(as.Date(daily_data$date, format="%d/%m/%Y")),]

- 9,293
- 12
- 65
- 148

- 73
- 1
- 1