Subset dataframe where date is within x days of a vector of dates in R -
i have vector of dates e.g.
dates <- c('2013-01-01', '2013-04-02', '2013-06-10', '2013-09-30') and dataframe contains 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 would subset dataframe contains rows date less 5 days after of dates in 'dates' vector.
i.e. initial dataframe looks this
date b 2013-01-04 1 2013-01-22 2 b 2013-10-01 3 c 2013-10-10 4 d after query left first , third row (since 2013-01-04 within 5 days of 2013-01-01 , 2013-10-01 within 5 days of 2013-09-30)
does know of best way this?
thanks in advance
this easy (and fast) data.table roll:
library(data.table) dt = data.table(df) # convert date (or idate) have numbers instead of strings dates # set key dates join dt[, date := as.date(date)] dates = data.table(date = as.date(dates), key = 'date') # join roll of 5 days, throwing out dates don't match dates[dt, roll = 5, nomatch = 0] # date b #1: 2013-01-04 1 #2: 2013-10-01 3 c
Comments
Post a Comment