Perform vector-based operation on data.frame by group

后端 未结 1 982
醉酒成梦
醉酒成梦 2021-01-22 03:34

I have seen some fairly similar questions answered here, however, my brain is having some trouble making the leap to apply what I read directly to my data.

I would like

相关标签:
1条回答
  • 2021-01-22 04:11

    One way is join on 'input' and then assign the value to 'output

    library(data.table)
    setDT(dfMeasured)[dfRef, output := output - i.output,on = .(input)]
    dfMeasured
    #    input output group
    # 1:     0      0     A
    # 2:     1     -2     A
    # 3:     2      1     A
    # 4:     3      0     A
    # 5:     0     -2     B
    # 6:     1     -4     B
    # 7:     2     -3     B
    # 8:     3      7     B
    # 9:     0     -3     C
    #10:     1     -3     C
    #11:     2     -6     C
    #12:     3      8     C
    
    0 讨论(0)
提交回复
热议问题