How to update multiple columns in data.table with values from matrix. Here is the MWE illustrating the problem I am facing:
library(data.table) DT = data.table(expand.grid(1:3,1:3,1:3)) DF = expand.grid(1:3,1:3,1:3) mat = matrix(seq(0, 80), 27, 3)
In the world of data.frame I would go with this syntax:
DF[,2:ncol(DF)] = mat[,2:ncol(DF)]
A similar adoption of the data.table syntax gives a few warnings with a very strange result.
DT[,2:ncol(DF) := mat[,2:ncol(DF)], with=FALSE]
This is clearly a mistake, as warnings show that the matrix was actually smoothed. Warning Messages:
1: In `[.data.table`(DT, , `:=`(2:ncol(DF), mat[, 2:ncol(DF)]), with = FALSE) : 2 column matrix RHS of := will be treated as one vector
source share