Subset Data Frame Between Two Dates in R (Example)

 

In this R post you’ll learn how to select data frame rows in a particular date range.

The article will contain the following information:

Let’s do this:

 

Creating Example Data

The following data is used as basement for this R programming tutorial:

data <- data.frame(date = c("2022-01-05",      # Create example data
                            "2020-03-13",
                            "2025-07-15",
                            "2019-11-22",
                            "2022-04-15",
                            "2021-12-08"),
                   values = 1:6)
data                                           # Print example data

 

table 1 data frame subset data frame between two dates r

 

Have a look at the table that has been returned after running the previous R programming code.

It illustrates that our example data has six rows and two variables. The first variable contains dates and the second variable some values.

 

Example: Extract Date Range of Data Frame Using as.Date() Function & Logical Operators

In this example, I’ll show how to select data frame rows based on a specific date range.

The first step for this is that we have to check whether our date column has the Date class:

class(data$date)                               # Check class of date column
# [1] "character"

In our case, the date column has the character class. For that reason, we have to apply the as.Date function to convert our column to the data type Date:

data$date <- as.Date(data$date)                # Convert character to date

Let’s check the class of our updated date column:

class(data$date)                               # Check class of date column
# [1] "Date"

The RStudio console returns the data type Date – looks good.

Next, we can use logical operators to keep only rows that lie within a data range:

data_new <- data[data$date > "2021-01-01" &    # Extract data frame subset
                   data$date < "2024-01-01", ]
data_new                                       # Print new data frame

 

table 2 data frame subset data frame between two dates r

 

Table 2 shows the output of the previous R code: A subset of our input data frame with rows in a certain date range.

 

Video & Further Resources

Do you want to learn more about data frames and dates? Then you might have a look at the following video of my YouTube channel. In the video, I’m explaining the R codes of this tutorial:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Besides that, you could have a look at the related posts that I have published on this website.

 

You have learned in this article how to extract certain data frame rows based on a sequence of dates in a column in R programming. Please let me know in the comments section, in case you have further questions.

 

Subscribe to the Statistics Globe 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.

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

Menu
Top