This is similar to Update values in data.table with values from another data.table and R data.table replacing an index of values from another data.table, except in my situation
This code should work in the current format given your criteria.
dt_original[dt_newdata, names(dt_newdata) := Map(pmax, mget(names(dt_newdata)), dt_newdata)]
It joins to the IDs that match between the data.tables and then performs an assignment using :=
Because we want to return a list, I use Map
to run pmax
through the columns of data.tables matching by the name of dt_newdata. Note that it is necessary that all names of dt_newdata are in dt_original data.
Following Frank's comment, you can remove the first column of the Map
list items and the column names using [-1]
because they are IDs, which don't need to be computed. Removing the first column from Map
avoids one pass of pmax
and also preserves the key on id. Thanks to @brian-stamper for pointing out the key preservation in the comments.
dt_original[dt_newdata,
names(dt_newdata)[-1] := Map(pmax,
mget(names(dt_newdata)[-1]),
dt_newdata[, .SD, .SDcols=-1])]
Note that the use of [-1]
assumes that the ID variable is located in the first position of new_data. If it is elsewhere, you could change the index manually or use grep
.