You are currently viewing How to Sort by Date in R?

How to perform a sort by date in R? I will quickly explain sorting data frame rows based on the date column in this article. R provides order() from the base function, arrange() from the dplyr package, and the setorder() from the data.table package to sort data frame in R.

Note that examples or methods used in this article are also used to sort columns of any type, not specific to just date.

1. Prepare Data with Date Column

Let’s create a data frame with a date column. Since we need date values in date data type, I have used as.Date() function to convert a vector of strings to date.


# Create Data Frame
df=data.frame(id=c(11,22,33,44,55),
          name=c("spark","python","R","jsp","java"),
          price=c(144,NA,321,567,567),
          publish_date= as.Date(
            c("2007-06-22", "2004-02-13", "2006-05-18",
              "2010-09-02","2007-07-20"))
          )
df

Yields below output

r sort by date

2. Sort by Date by Base R

Let’s use the order() function to sort the data frame by the date column. In my dataset, the publish_date column is of type date, so we’ll use it to sort the data frame.


# Sort Data Frame by date column
df2 <- df[order(df$publish_date),]
df2

Yields below output. Notice the date column publish_date which is sorted in ascending order.

To sort date columns in descending order use decreasing=TRUE.


# Sort in descending order
df2 <- df[order(df$publish_date, decreasing=TRUE),]
df2

3. Sort Rows by Date in R using arrange()

The arrange() function from the dplyr package sorts values of the data frame. If you want to use the arrange() function, you first need to install the dplyr package with install.packages(‘dplyr’) and then load it with library(dplyr).

All dplyr package functions allow a data frame as their starting parameter. When working with the dplyr package, we often use the Infix operator %>% from the magrittr package. This operator directs the output from the left-hand side to serve as the first parameter of the function on the right-hand side. For instance, when using x %>% f(y), it translates to f(x, y), channeling the outcome from the left to the right.


# Load dplyr library
library(dplyr)
df2 <- df %>% arrange(publish_date)
df2

To sort a date column in descending order


# Sort descending order
df2 <- df %>% arrange(desc(publish_date))
df2

4. By using setorder()

The setorder() function of data.table package to sort a data frame by a date column. This function allows the data frame object and the column to sort by as input and returns a new data frame sorted by paricular date column.


# Load data.table library
library("data.table")
df2 <- setorder(df,publish_date)
df2

5. Conclusion

In this article, you have learned how to sort the date column in the data frame by ascending and descending order with the help of the order() function, arrange from the dplyr package, and the setorder() from the data.table package.