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
  • 42,459
  • 4
  • 77
  • 117
Mikołaj
  • 385
  • 4
  • 17

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
  • 874,273
  • 37
  • 540
  • 662