I have two data tables. I'm merging them in 4 common columns (Main key=col1). However, many rows in dt1 have NA in the 3 subkeys (col2, col3, col4). When merging, they keep NA instead of taking the existing value from the row in dt2 that they were matched with.
Example:
setkey(dt1, letter, number)
setkey(dt2, letter, number)
dt1 dt2
letter number size letter number color
a 10 big a 10 blue
b NA small b 20 orange
c 30 big c 30 yellow
d 40 big d 40 red
dt_merged <- merge(dt1, dt2, all=TRUE)
dt_merged
letter number size color
a 10 big blue
b NA small orange
c 30 big yellow
d 40 big red
How can I condition the merge to take values from dt2 for these 3 columns (ex. col2,col3,col4) whenever dt1 has NA?
Edit: Added size column, since it previously seemed unnecessary to merge the DTs with the given values.