I have a vector of dates e.g.
dates <- c('2013-01-01', '2013-04-02', '2013-06-10', '2013-09-30')
And a dataframe which contains a date column e.g.
df <- data.frame(
'date' = c('2013-01-04', '2013-01-22', '2013-10-01', '2013-10-10'),
'a' = c(1,2,3,4),
'b' = c('a', 'b', 'c', 'd')
)
And I would would like to subset the dataframe so it only contains rows where the date is less than 5 days after any of the dates in the 'dates' vector.
i.e. The initial dataframe looks like this
date a b
2013-01-04 1 a
2013-01-22 2 b
2013-10-01 3 c
2013-10-10 4 d
After the query I would only be left with the first and third row (since 2013-01-04 is within 5 days of 2013-01-01 and 2013-10-01 is within 5 days of 2013-09-30)
Does anyone know of the best way to do this?
Thanks in advance