Опция, использующая неэквивалентное объединение в пакете data.table
:
#calculate date one year after surgery
surgery_7[, oneyr := as.IDate(sapply(surgery_date, function(x)
seq(x, by="1 year", length.out=2L)[2L]))]
#update by reference
surgery_7[, post_op_visits :=
#non-equi join
visits_1[.SD, on=.(PatientProfileId, visit_date>=surgery_date, visit_date<=oneyr),
#for each row of surgery_7 find the number of rows from visits_1
by=.EACHI, .N]$N]
output surgery_7
:
PatientProfileId surgery_date oneyr post_op_visits
1: 1 2018-01-01 2019-01-01 2
2: 2 2019-01-01 2020-01-01 1
data:
library(data.table)
surgery_7 <- data.table(PatientProfileId=c(1,2),
surgery_date=as.IDate(c("2018-01-01", "2019-01-01")))
# PatientProfileId surgery_date
#1: 1 2018-01-01
#2: 2 2019-01-01
visits_1 <- data.table(PatientProfileId=c(1,1,1,2,2),
visit_date=as.IDate(c("2018-03-15","2018-09-15","2019-02-03","2019-06-30","2020-01-15")))
# PatientProfileId visit_date
# 1: 1 2018-03-15
# 2: 1 2018-09-15
# 3: 1 2019-02-03
# 4: 2 2019-06-30
# 5: 2 2020-01-15