0

I have a data table containing 4 variables. How can I add two columns using indexes.

As an example:

DT = data.table(a=6,b=8,c=0,d=5)
min1 = 3
min2 = 4
DT[,"mrg":=min1+min2,with=F] # How to  write this line correctly
DT[,(c(min1,min2)):=NULL] # this works
Gaurav Singhal
  • 998
  • 2
  • 10
  • 25
  • It's generally discouraged to refer to cols by number. See, for example, the answer to the first question in the FAQ https://rawgit.com/wiki/Rdatatable/data.table/vignettes/datatable-faq.html – Frank Nov 04 '16 at 15:12

1 Answers1

3

You can use .SDcols:

DT[, "mrg" := Reduce("+", .SD), .SDcols = c(min1, min2)]
Roland
  • 127,288
  • 10
  • 191
  • 288