Consider the following random MWE.
For every row, I am trying to determine which variable has the closest value below the constant reference_day and which variable has the closest value above the constant reference_day.
df1 <-
structure(
list(id = 1:5,
gender = c("female", "male", "male", "male", "male"),
reference_day = structure(c(18052, NA, 18052, 18052, 18052), class = "Date"),
var1 = structure(c(16505, 17144, 18139, NA, 16639), class = "Date"),
var2 = structure(c(NA, 18042, 16544, 16697, NA), class = "Date"),
var3 = structure(c(17845, 18070, 17152, 16571, NA), class = "Date")),
row.names = c(NA, -5L), class = "data.frame")
df1
id gender reference_day var1 var2 var3
1 1 female 2019-06-05 2015-03-11 <NA> 2018-11-10
2 2 male <NA> 2016-12-09 2019-05-26 2019-06-23
3 3 male 2019-06-05 2019-08-31 2015-04-19 2016-12-17
4 4 male 2019-06-05 <NA> 2015-09-19 2015-05-16
5 5 male 2019-06-05 2015-07-23 <NA> <NA>
The result I'm after is this:
id gender reference_day var1 var2 var3 closest_to_left closest_to_right
1 1 female 2019-06-05 2015-03-11 <NA> 2018-11-10 var3 <NA>
2 2 male <NA> 2016-12-09 2019-05-26 2019-06-23 <NA> <NA>
3 3 male 2019-06-05 2019-08-31 2015-04-19 2016-12-17 var3 var1
4 4 male 2019-06-05 <NA> 2015-09-19 2015-05-16 var2 <NA>
5 5 male 2019-06-05 2015-07-23 <NA> <NA> var1 <NA>
After much trial and error, I was actually able to find a solution to this with the case_when function of dplyr, but it took an insane amount of boilerplate code, which lead me to think that there just has to be a more clever solution.
I personally prefer to work with dplyr, but any help is greatly appreciated.