Table of Contents
The process of subsetting a dataset by a specific date range in R involves selecting a subset of data within a specified date range from a larger dataset. This can be achieved by using different functions and operators in R, such as the subset() function or the “[” operator. Examples of subsetting a dataset by a specific date range in R include selecting data from a certain month, year, or a specific date range within a larger dataset. This process can be useful for filtering and analyzing data within a specific time frame.
Subset by a Date Range in R (With Examples)
The easiest way to subset a data frame by a date range in R is to use the following syntax:
df[df$date >= "some date" & df$date <= "some date", ]
This tutorial provides several examples of how to use this function in practice.
Example 1: Subset Between Two Dates
The following code shows how to select the rows of a data frame that fall between two dates, inclusive:
#make this example reproducible
set.seed(0)
#create data frame
df <- data.frame(date = as.Date("2021-01-01") - 0:19,
sales = runif(20, 10, 500) + seq(50, 69)^2)
#view first six rows
head(df)
date sales
1 2021-01-01 2949.382
2 2020-12-31 2741.099
3 2020-12-30 2896.341
4 2020-12-29 3099.698
5 2020-12-28 3371.022
6 2020-12-27 3133.824
#subset between two dates, inclusive
df[df$date >= "2020-12-25" & df$date <= "2020-12-28", ]
date sales
5 2020-12-28 3371.022
6 2020-12-27 3133.824
7 2020-12-26 3586.211
8 2020-12-25 3721.891
You only need to modify the greater and less than signs to select the rows that fall between two dates, exclusive:
#make this example reproducible
set.seed(0)
#create data frame
df <- data.frame(date = as.Date("2021-01-01") - 0:19,
sales = runif(20, 10, 500) + seq(50, 69)^2)
#subset between two dates, exclusive
df[df$date > "2020-12-25" & df$date < "2020-12-28", ]
date sales
6 2020-12-27 3133.824
7 2020-12-26 3586.211
Example 2: Subset After a Certain Date
The following code shows how to select the rows of a data frame that occur after a certain date:
#make this example reproducible
set.seed(0)
#create data frame
df <- data.frame(date = as.Date("2021-01-01") - 0:19,
sales = runif(20, 10, 500) + seq(50, 69)^2)
#subset after a certain date
df[df$date >= "2020-12-22", ]
date sales
1 2021-01-01 2949.382
2 2020-12-31 2741.099
3 2020-12-30 2896.341
4 2020-12-29 3099.698
5 2020-12-28 3371.022
6 2020-12-27 3133.824
7 2020-12-26 3586.211
8 2020-12-25 3721.891
9 2020-12-24 3697.791
10 2020-12-23 3799.266
11 2020-12-22 3640.275Example 3: Subset Before a Certain Date
The following code shows how to select the rows of a data frame that occur before a certain date:
#make this example reproducible
set.seed(0)
#create data frame
df <- data.frame(date = as.Date("2021-01-01") - 0:19,
sales = runif(20, 10, 500) + seq(50, 69)^2)
#subset before a certain date
df[df$date < "2020-12-22", ]
date sales
12 2020-12-21 3831.928
13 2020-12-20 3940.513
14 2020-12-19 4315.641
15 2020-12-18 4294.211
16 2020-12-17 4612.222
17 2020-12-16 4609.873
18 2020-12-15 4850.633
19 2020-12-14 5120.034
20 2020-12-13 4957.217How to Plot a Time Series in R
How to Extract Year from Date in R
How to Aggregate Daily Data to Monthly and Yearly in R
Cite this article
stats writer (2024). How can I subset a dataset by a specific date range in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-subset-a-dataset-by-a-specific-date-range-in-r-can-you-provide-some-examples/
stats writer. "How can I subset a dataset by a specific date range in R?." PSYCHOLOGICAL SCALES, 22 Apr. 2024, https://scales.arabpsychology.com/stats/how-can-i-subset-a-dataset-by-a-specific-date-range-in-r-can-you-provide-some-examples/.
stats writer. "How can I subset a dataset by a specific date range in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-subset-a-dataset-by-a-specific-date-range-in-r-can-you-provide-some-examples/.
stats writer (2024) 'How can I subset a dataset by a specific date range in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-subset-a-dataset-by-a-specific-date-range-in-r-can-you-provide-some-examples/.
[1] stats writer, "How can I subset a dataset by a specific date range in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, April, 2024.
stats writer. How can I subset a dataset by a specific date range in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
