r - .SD and .SDcols for the i expression in data.table join -
i'm trying copy subset of columns y x based on join, subset of columns dynamic
i can identify columns quite easily: names(y)[grep("xxx", names(y))] when try use code in j expression, gives me column names, not values of columns. .sd , .sdcols gets pretty close, apply x expression. i'm trying this:
x[y[names(y)[grep("xxx", names(y))] := .sd, .sdcols = names(y)[grep("xxx", names(y)), on=.(zzz)]
is there equivalent set of .sd , .sdcols constructs apply i expression? or, need build string j expression , eval string?
perhaps started:
library(data.table) x <- as.data.table(mtcars[1:5], keep.rownames = "id") y <- as.data.table(mtcars, keep.rownames = "id") cols <- c("gear", "carb") # copy cols y x based on common "id": x[y, (cols) := mget(cols), on = "id"] as frank notes in comment, might safer prefix column names i. ensure assigned columns indeed y:
x[y, (cols) := mget(paste0("i.", cols)), on = "id"]
Comments
Post a Comment