# NOT RUN {
data1 <- data.frame(id = 1:10,
entry.dt = as.Date(paste("2011", 1:10, "5", sep='-')))
temp1 <- c(1,4,5,1,3,6,9, 2,7,8,12,4,6,7,10,12,3)
data2 <- data.frame(id = c(1,1,1,2,2,4,4,5,5,5,6,8,8,9,10,10,12),
lab.dt = as.Date(paste("2011", temp1, "1", sep='-')),
chol = round(runif(17, 130, 280)))
#first cholesterol on or after enrollment
indx1 <- neardate(data1$id, data2$id, data1$entry.dt, data2$lab.dt)
data2[indx1, "chol"]
# Closest one, either before or after.
#
indx2 <- neardate(data1$id, data2$id, data1$entry.dt, data2$lab.dt,
best="prior")
ifelse(is.na(indx1), indx2, # none after, take before
ifelse(is.na(indx2), indx1, #none before
ifelse(abs(data2$lab.dt[indx2]- data1$entry.dt) <
abs(data2$lab.dt[indx1]- data1$entry.dt), indx2, indx1)))
# closest date before or after, but no more than 21 days prior to index
indx2 <- ifelse((data1$entry.dt - data2$lab.dt[indx2]) >21, NA, indx2)
ifelse(is.na(indx1), indx2, # none after, take before
ifelse(is.na(indx2), indx1, #none before
ifelse(abs(data2$lab.dt[indx2]- data1$entry.dt) <
abs(data2$lab.dt[indx1]- data1$entry.dt), indx2, indx1)))
# }
Run the code above in your browser using DataLab