Subset dataframe where date is within x days of a vector of dates in R

一个人想着一个人 提交于 2019-12-04 05:37:55

This is easy (and very fast) to do with a data.table roll:

library(data.table)
dt = data.table(df)

# convert to Date (or IDate) to have numbers instead of strings for dates
# also set the key for dates for the join
dt[, date := as.Date(date)]
dates = data.table(date = as.Date(dates), key = 'date')

# join with a roll of 5 days, throwing out dates that don't match
dates[dt, roll = 5, nomatch = 0]
#         date a b
#1: 2013-01-04 1 a
#2: 2013-10-01 3 c

broken down into steps:

# Rows Selected: Iterate over each row in the DF, 
#   and check if its `date` value is within 5 from any value in the `dates` vector
rows <- sapply(df$date, function(x) any( abs(x-dates) <=  5))

# Use that result to subset your data.frame
df[rows, ]

#         date a b
# 1 2013-01-04 1 a
# 3 2013-10-01 3 c

Importantly, make sure your date values are actual Dates and not characters looking like dates

dates <- as.Date(dates)
df$date <- as.Date(df$date)

First make sure that df$date is of class date. Then:

df[df$date %in% sapply(dates, function(x) x:(x+5)),]

        date a b
1 2013-01-04 1 a
3 2013-10-01 3 c

For some reason I feel like this may be a more proper method:

 df[df$date %in% mapply(`:`, from=dates, to=dates+5),]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!