0

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)
r2evans
  • 141,215
  • 6
  • 77
  • 149
r_newbie
  • 13
  • 2
  • Sample dataset: **Create df1** `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)%>%view()` **Create df2** `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) df2 %>% view()` Have tried `Merge(df1, df2, by=”id”, all.x=T), but here it only keeps first sample date with multiple sx_dates` – r_newbie Apr 20 '23 at 17:58
  • 1
    @akrun, its available now, right? – r_newbie Apr 20 '23 at 18:08

0 Answers0