Data.table column number in .SDcols

I want to increase each column specified in .SDcols by column of each column, maybe?

I think it is possible to get information about col type columns.

+4
source share
1 answer

We can use Mapfor this. Suppose that if we change the first and second columns ('i1'), specify it in .SDcols, and when Mapwe use the function for each column of the subset Data.table ( .SD) using the corresponding index specified in "i1", and assign ( :=) the output into columns.

i1 <- 1:2
dt1[, (i1) := Map(`^`, .SD, i1), .SDcols = i1]

data

dt1 <- data.table(a = 1:5, b= 6:10, c = 11:15)
+5
source

Source: https://habr.com/ru/post/1655379/


All Articles