Order Data Frame by Date in R (Example)

 

In this tutorial, I’ll explain how to sort a data frame based on a date column in the R programming language.

The article will contain the following content:

It’s time to dive into the example…

 

Example Data

Let’s first create some example data in R:

data <- data.frame(dates = c("2020-04-03",    # Create example data
                             "2022-12-05",
                             "2019-01-06",
                             "2020-07-11",
                             "2021-07-23",
                             "2021-03-13"),
                   x = 1:6)
data                                          # Print example data
#        dates x
# 1 2020-04-03 1
# 2 2022-12-05 2
# 3 2019-01-06 3
# 4 2020-07-11 4
# 5 2021-07-23 5
# 6 2021-03-13 6

The previous RStudio console output shows the structure of our example data: It contains six rows and two columns. The first variable contains some dates and the second variable a range of numeric values.

 

Example: Sorting Data Frame by Date Column Using as.Date Function

This section illustrates how to rearrange our data frame rows according to our date column.

For this, the data class of our date column is important. Let’s check the current data type using the class function:

class(data$dates)                             # Check class of date column
# "factor"

Our date variable has the factor class.

In order to sort our data frame based on our date column, we have to convert our dates from the factor to the Date class using the as.Date function.

data$dates <- as.Date(data$dates,             # Change class of date column
                      format = "%Y-%m-%d")

Let’s check the class of our date variable again:

class(data$dates)                             # Check class of date column
# "Date"

Now it has the Date class – looks good.

Next, we can apply the order function to sort our data frame according to the date column:

data <- data[order(data$dates), ]             # Order data by date
data                                          # Print updated data
#        dates x
# 3 2019-01-06 3
# 1 2020-04-03 1
# 4 2020-07-11 4
# 6 2021-03-13 6
# 5 2021-07-23 5
# 2 2022-12-05 2

As you can see based on the previous output of the RStudio console, our data frame is now ordered by dates.

 

Video & Further Resources

Have a look at the following video of my YouTube channel. In the video, I’m explaining the R syntax of this page:

 

The YouTube video will be added soon.

 

In addition, I can recommend to have a look at the related tutorials of my website:

 

In this tutorial, I showed how to rearrange the rows of a data matrix by dates in the R programming language. Let me know in the comments section, if you have further questions or comments.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top