I have a df that looks like following. I would like to find the closest date (from R_1:R_10) that happened before date. Is it a way to calculate the difference in days for R1:R10 to date at once and then pick out the the value we want, and then put it in a new variable cdate?
the output will looks like this:
the cdate will be identified by R_1:R10-date, whichever has the greatest negative value will be the date that we put in cdate
Sample data:
df<-structure(list(id = c("A0003", "A0003", "A0003", "A0003", "A0003",
"A0003", "A0005", "A0005"), date = structure(c(17774, 17799,
17805, 17837, 17846, 17873, 17784, 17784), class = "Date"), R_1 = structure(c(17773,
17773, 17773, 17773, 17773, 17773, 17785, 17785), class = "Date"),
R_2 = structure(c(17815, 17815, 17815, 17815, 17815, 17815,
17827, 17827), class = "Date"), R_5 = structure(c(17794,
17794, 17794, 17794, 17794, 17794, 17806, 17806), class = "Date"),
R_10 = structure(c(17871, 17871, 17871, 17871, 17871, 17871,
NA, NA), class = "Date")), row.names = c(NA, 8L), class = "data.frame")

