Summing across rows of a data.table for specific columns

后端 未结 2 1591
日久生厌
日久生厌 2020-12-15 07:40

I have a large data table (from the package data.table) with over 60 columns (the first three corresponding to factors and the remaining to response variables, in this case

2条回答
  •  被撕碎了的回忆
    2020-12-15 08:00

    [ Edited 2020-02-15 to reflect current state of data.table ] In recent versions of data.table rowSums(Abundance[ , 4:6]) works as OP originally expected. Here are some alternatives:

    Abundance[, SumAbundance := rowSums(.SD), .SDcols = 4:6]
    

    Also, I didn't check, but I have a suspicion this will be faster, since it will not convert to matrix as rowSums does:

    Abundance[, SumAbundance := Reduce(`+`, .SD), .SDcol = 4:6]
    

提交回复
热议问题