Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.table column number in .SDcols

Tags:

r

data.table

I want to exponentiate every column specified in .SDcols, by the column number of every column, is that possible?

I think it's possible if one can get information about the columns like col number.

like image 921
Juan Carlos Avatar asked Sep 21 '16 18:09

Juan Carlos


1 Answers

We can use Map to do this. Suppose, if we are changing the first and second columns ('i1'), specify it in the .SDcols, and with Map we apply the function on each column of Subset of Data.table (.SD) with the corresponding index specified in 'i1' and assign (:=) the output to the columns.

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

data

dt1 <- data.table(a = 1:5, b= 6:10, c = 11:15)
like image 106
akrun Avatar answered Sep 28 '22 03:09

akrun