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.


4 Comments. Leave new

  • Consider that my data frame has two columns. Time appears in the first column. I create time as follows:
    start_date <- as.Date("2018/01/01")
    time <- seq(start_date, by = "day", length.out = 20)
    the second column's name as "value." How can I make a subset of a data frame that only contains the month of January?

    Reply
    • Hello Saima,

      I suggest the following solution:

      library(lubridate)
       
      start_date <- as.Date("2018/01/01")
       
      data<-data.frame(time=seq(start_date, by = "week", length.out = 20),
                       month=month(seq(start_date, by = "week", length.out = 20)))
      head(data)
      #         time month
      # 1 2018-01-01     1
      # 2 2018-01-08     1
      # 3 2018-01-15     1
      # 4 2018-01-22     1
      # 5 2018-01-29     1
      # 6 2018-02-05     2
       
      data_sub<-data[data$mont==1,]
      data_sub
      #         time month
      # 1 2018-01-01     1
      # 2 2018-01-08     1
      # 3 2018-01-15     1
      # 4 2018-01-22     1
      # 5 2018-01-29     1

      Be aware that I use the “week” interval instead of “day”. Because when you use “day” for 20 dates, you get only dates in January either way.

      Regards,
      Cansu

      Reply

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.

Top