I have a dataframe, df1, with ID and a surgery date, sx_date. Same patients can have multiple sx_dates.
In another dataframe, df2, I have ID and sample dates. Again, same patient can have multiple samles taken and thereby repeated dates for same ID. In this I have a third column describing name of sample.
I would like to describe, how many samples(from df2) were taken within 30 days from every sx_date from df1. I know last step is to calculate difference between the two dates. But how to merge accordingly?
id <- c(1, 2, 3, 4)
date <- seq(as.Date("2010-01-01"), as.Date("2010-10-09"), by = "6 days")
df1 <- expand.grid(id = id, date = date)
id <- c(1, 2, 3, 4)
date <- seq(as.Date("2021-01-20"), as.Date("2021-11-09"), by = "4 days")
sample <- rep(c("hgb","crp","Na","K"),each=5)
df2 <- expand.grid(id = id, date = date, sample = sample)
df2 <- df2 %>% arrange(id)