how to use merge() to update a table in R

前端 未结 6 713
孤独总比滥情好
孤独总比滥情好 2020-11-27 20:47

I\'m trying to figure out how to use merge() to update a database.

Here is an example. Take for example the data frame foo



        
6条回答
  •  忘掉有多难
    2020-11-27 21:33

    The optimal solution using data.table

    library(data.table)
    setDT(foo)
    setDT(bar)
    foo[bar, on="index", value:=i.value]
    foo
    #   index value
    #1:     a   100
    #2:     b   101
    #3:     c   200
    #4:     d   201
    

    first argument in [ data.table method is named i thus we can refer to column from table in i argument using i. prefix.

提交回复
热议问题