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
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 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.
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.
- Convert Factor to Date in R
- Order Data Frame by Date in R
- Add and Subtract Days to from Date
- Change Format of Dates in R
- Number of Months Between Two Dates
- All R Programming Tutorials
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.
Statistics Globe Newsletter
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?
Hello Saima,
I suggest the following solution:
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
Awesome.
You are welcome 🙂