Hopefully this diagram makes it clearer. I'd like to create a_2, b_2, and c_2 columns in df2 that are composed of specific values from df1's a_2, b_2, and c_2 columns. For each df2 row, we find the row in df1 closest in terms of a_1 that also matches the must_match column. When we find this row in df1, we extract its a_2 value and insert it into df2's a_2 column. We do this for every row in df2. My purrr code below successfully accomplishes everything so far. However, I'd like to repeat this process to create b_2, c_2, and potentially more other columns; creating b_2 would mean finding the closest b_1 value in df1 among matching must_match rows, c_2 would mean finding the closest c_1 value in df1 among matching must_match rows, and so on.
library(DescTools)
df1 <- data.frame(must_match = c(0, 1, 0, 1, 0),
c_2 = c(8.3, 6.2, 6.1, 4.5, 1.5),
c_1 = c(4, 1, 3, 9, 5),
b_2 = c(5, 3, 9, 7, 11),
b_1 = c(20, 22, 24, 26, 27),
a_2 = c(5.3, 5.4, 3.1, 4.2, 7.2),
a_1 = c(2, 7, 9, 6, 11))
df2 <- data.frame(must_match = c(0, 1, 0, 1, 0, 1),
a_1 = c(7, 2, 5, 11, 1, 6),
b_1 = c(6, 5.8, 1.6, 9.7, 6.2, 12.8),
c_1 = c(2.3, 5.3, 1.2, 7.3, 7.8, 8.2))
pmap_dbl(df2, function(a_1, must_match, ...){
df1 %>%
pull(a_2) %>%
nth(which(df1$a_1 == Closest(df1$a_1[which(df1$must_match %in% must_match)], a_1))) # finds row in df1 that 1) matches in `must_mattch` column and 2) is closest in `a_1`
# granted this output is a vector, but converting to a column should be easy enough
})