1 | initial version |
Dates can be filtered in R using the subset()
function or the dplyr
package.
Using subset()
function:
# Creating a dataframe with dates
data <- data.frame(
date = as.Date(c("2021-01-01", "2021-01-02", "2021-01-03", "2021-01-04")),
value = c(10, 20, 30, 40)
)
# Filtering dates between Jan 2 and Jan 3
subset(data, date >= as.Date("2021-01-02") & date <= as.Date("2021-01-03"))
Using dplyr
package:
library(dplyr)
# Creating a dataframe with dates
data <- data.frame(
date = as.Date(c("2021-01-01", "2021-01-02", "2021-01-03", "2021-01-04")),
value = c(10, 20, 30, 40)
)
# Filtering dates between Jan 2 and Jan 3
data %>% filter(date >= as.Date("2021-01-02") & date <= as.Date("2021-01-03"))