r Replace only some table values with values from alternate table

后端 未结 4 1784
失恋的感觉
失恋的感觉 2021-01-27 02:45

This is not a \"vlookup-and-fill-down\" question.

My source data is excellent at delivering all the data I need, just not in in a usable form. Recent changes in volume

4条回答
  •  误落风尘
    2021-01-27 03:23

    In data.table world, this is an "update-join". Join on "Item" and then update the values in the original set with the values from the new set:

    library(data.table)
    setDT(inv)
    setDT(svc)
    
    inv[svc, on="Item", c("onPO","onHand") := .(i.Backordered, `i.Rcv'd`)]
    
    #inv   original table
    #svc   update table
    #on=   match on specified variable
    # :=   overwrite  onPO    with  Backordered
    #                 onHand  with  Rcv'd
    
    
    #          Item onHand demand onPO
    # 1:   10100200    600   3300 2700
    # 2:   10100201      0     NA   20
    # 3:   10100202     39     40    1
    # 4:   10100203      0     40   40
    # 5:   10100204      0     NA  100
    # 6: 10100205-A     44     NA   18
    # 7:   10100206     40     70   30
    # 8:   10100207      0    126  126
    # 9:   10100208      0     10   10
    #10:   10100209      0     10   10
    #11:   10100210      0    250  250
    

提交回复
热议问题