0

let's have a data frame:

date <- c("2019-01-03", "2017-02-03", "2018-12-30", "2018-08-12")
date <- as.Date(date, format = "%Y-%m-%d")
variable <- c(1,2, 100, 500)
df <- data.frame(date, variable)

And special, specific date:

special_date <- c("2019-01-04")
special_date <- as.Date(special_date, format = "%Y-%m-%d")

How to remove all rows with dates older than 30 days of special_date?

#expected outcome
#        date variable
#1 2019-01-03        1
#3 2018-12-30      100
alistaire
  • 40,464
  • 4
  • 71
  • 108
Mikołaj
  • 396
  • 3
  • 16

1 Answers1

1

We can use a logical condition in subset from base R

subset(df, date > (special_date - 30))
#          date variable
#1 2019-01-03        1
#3 2018-12-30      100

Or with filter from tidyverse

library(tidyverse)
df %>%
   filter(date > (special_date - 30))
akrun
  • 789,025
  • 32
  • 460
  • 575