Вот вариант:
cols <- paste0("col", 2L:4L)
#look up the missing values from dt2 first before merging
dt1[is.na(col2), (cols) := dt2[.SD, on=.(col1), mget(paste0("x.", cols))]]
merge(dt1, dt2, all=TRUE)
вывод:
col1 col2 col3 col4 size color
1: a 10 10 10 big blue
2: b 20 20 20 small orange
3: c 30 30 30 big yellow
4: d 40 40 40 big red
данные:
dt1 <- data.table(col1 = c("a", "b", "c", "d"),
col2 = c(10L, NA, 30L, 40L),
col3 = c(10L, NA, 30L, 40L),
col4 = c(10L, NA, 30L, 40L),
size = c("big", "small", "big", "big"))
dt2 <- data.table(col1 = c("a", "b", "c", "d"),
col2 = c(10L, 20L, 30L, 40L),
col3 = c(10L, 20L, 30L, 40L),
col4 = c(10L, 20L, 30L, 40L),
color = c("blue", "orange", "yellow", "red"))