Row sums over columns with a certain pattern in their name

后端 未结 3 1323
梦毁少年i
梦毁少年i 2020-12-03 01:33

I have a data.table like this

dput(DT)
structure(list(ref = c(3L, 3L, 3L, 3L), nb = 12:15, i1 = c(3.1e-05, 
0.044495, 0.82244, 0.322291), i2 = c(0.000183, 0.1         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 02:05

    Use .SDcols to specify the columns, then take rowSums. Use := to assign new columns:

    DT[ ,sum := rowSums(.SD), .SDcols = grep("i", names(DT))]
    

提交回复
热议问题