Вот решение с базой R с использованием некоторых методов dplyr.Это не самое чистое и лучшее решение, но оно должно решить вашу проблему.
df<-structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L),
DATE = structure(c(11323, 11354, 11396, 11432, 11471, 11444,
11383, 11404, 11416, 11447), class = "Date"), DRUG = structure(c(1L,
1L, 2L, 3L, 1L, 2L, 1L, 3L, 4L, 2L), .Label = c("A", "B",
"C", "D"), class = "factor")), row.names = c(NA, -10L), class = "data.frame")
#Note DATE was converted to a Date object with the following line
#df$DATE<-as.Date(df$DATE, "%m/%d/%Y")
date.event<-read.table(header=TRUE, text="ID date.event
1 1/20/2001
1 4/11/2001
2 3/1/2001")
date.event$date.event<-as.Date(date.event$date.event, "%m/%d/%Y")
library(dplyr)
#calculate the prev_drup by counting the number of unique drugs
df<-df %>% group_by(ID) %>% mutate(prev_drug= (cumsum(!duplicated(DRUG)))-1)
#loop through each row after spitting and filtering by ID
event.30d.prior<-sapply(1:nrow(df), function(i){
events<-date.event[date.event$ID==df$ID[i], "date.event"]
sum(between(events, df$DATE[i]-30, df$DATE[i]))
})
finalanswer<-cbind(df, event.30d.prior=unlist(event.30d.prior))