How to set multiple columns in a data table to values from different columns in the same data table?
You can use the :=
operator and name column numbers in .SD
:
dt[, 1:3 := .SD, .SDcols = 4:6]
> dt
X1 X2 X3 X4 X5 X6
1: 43 75 29 43 75 29
2: 88 17 1 88 17 1
3: 83 51 28 83 51 28
4: 32 10 81 32 10 81
5: 63 21 25 63 21 25
Perhaps a for loop would look better?
for (i in 1:3) dt[[i]] = dt[[i+3]]