Adding values in two data.tables

后端 未结 2 569
轻奢々
轻奢々 2020-12-19 06:54

I have two data.tables, and one has a subset of rows/columns of another. I\'d like to add values of the smaller data.table to the values of the larger one:

D         


        
2条回答
  •  执笔经年
    2020-12-19 07:42

    You can use rbindlist() to bring the two together, then sum the values based on rn

    rbindlist(list(DT1, DT2), fill=TRUE)[, lapply(.SD, sum, na.rm = TRUE), by = rn]
    #    rn a b c
    # 1:  a 0 4 1
    # 2:  b 1 5 0
    # 3:  c 1 1 3
    

提交回复
热议问题