How to Sort by Date in R?

How to perform a sort by date in R? In this article, I will quickly explain sorting DateFrame rows based on the date column. R provides order() from base function, arrange() from dplyr package, and setorder() from data.table package to sort DataFrame 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 dateframe 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
          publish_date= as.Date(
            c("2007-06-22", "2004-02-13", "2006-05-18",

Yields below output

r sort by date

2. Sort by Date in R using order()

By using order() function let’s sort the dataframe by a date column. In my dataset, publish_date is a column of type date hence, let’s use this date column to sort the data.frame.

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

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

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

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

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

The arrange() function from dplyr package is also used to arrange the values in an ascending or descending order. To use arrange() function, you have to install dplyr first using install.packages(‘dplyr’) and load it using library(dplyr).

All functions in dplyr package take data.frame as a first argument. When we use dplyr package, we mostly use the infix operator %>% from magrittr, it passes the left-hand side of the operator to the first argument of the right-hand side of the operator. For example, x %>% f(y) converted into f(x, y) so the result from the left-hand side is then “piped” into the right-hand side. 

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

To sort date column in descending order

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

4. By using setorder()

Use setorder() function from data.table to perform sorting on date column. This function takes the data.frame object and column as input and return a new DataFrame after sorting by the specified column (date).

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

5. Conclusion

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

Related Articles

Naveen (NNK)

Naveen (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium

Leave a Reply