I want to do what seems a straightforward application of mapply in a data table. I want to multiply a series of data table columns by the value in another column. Here's my function. y is the single column to multiply the values in the other columns by. xIn is a column name to do this operation over.
f.xRatio <- function(xIn, y) {return(y * (xIn + 1)/(xIn - 1))}
I have a data table with a column called GDPratio and some columns with names like x.food1, x.food2, etc. I put these column names into a variable called x with
x <- paste0("x.", foodNames)
I create another variable with the names of the new columns created with the function
xRatio <- paste0("xRatio.", foodNames)
Here are two versions of my attempt at using mapply to create the xRatio columns from the function.
dt[, (xRatio) := mapply(FUN = f.xRatio, xIn = .SD, y = GDPRatio), .SDcols = (x)]
dt[, (xRatio) := mapply(FUN = f.xRatio, xIn = .(x), y = GDPRatio)]
Neither works. I think the first is close. I'm hoping someone can point out the flaw(s) in my logic without me creating a reproducible example.