Data.table: update multiple columns in a matrix data table

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)] #Data frame approach 

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] #Data table approach 

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 
+4
source share
2 answers

You need to convert RHS to list , and an easy way to do this is to use as.data.table :

 DT[, 2:ncol(DT) := as.data.table(mat[,2:ncol(DT)])] 

with is not required here, since LHS is automatically output to column numbers.

+8
source

When assigning to multiple columns, the columns should be collected in the list:

 idx <- 2:ncol(DT) DT[,idx] <- lapply(idx, function(col) mat[,col]) 

The same syntax works for data.frame. This is non-standard in the data table (where set and := are idiomatic), but it still makes sense to modify the DT by reference, I think.

Idiomatic approach := :

 DT[,(idx) := lapply(idx, function(col) mat[,col])] 
+3
source

All Articles