R - Create a new variable where each observation depends on another table and other variables in the data frame

前端 未结 5 1216
暗喜
暗喜 2021-01-18 02:06

I have the two following tables:

df <- data.frame(eth = c(\"A\",\"B\",\"B\",\"A\",\"C\"),ZIP1 = c(1,1,2,3,5))
Inc <- data.frame(ZIP2 = c(1,2,3,4,5,6,7)         


        
5条回答
  •  被撕碎了的回忆
    2021-01-18 02:26

    Sure, it can be done in data.table:

    library(data.table)
    setDT(df)
    
    df[ melt(Inc, id.var="ZIP2", variable.name="eth", value.name="Inc"), 
      Inc := i.Inc
    , on=c(ZIP1 = "ZIP2","eth") ]
    

    The syntax for this "merge-assign" operation is X[i, Xcol := expression, on=merge_cols].

    You can run the i = melt(Inc, id.var="ZIP", variable.name="eth", value.name="Inc") part on its own to see how it works. Inside the merge, columns from i can be referred to with i.* prefixes.


    Alternately...

    setDT(df)
    setDT(Inc)
    df[, Inc := Inc[.(ZIP1), eth, on="ZIP2", with=FALSE], by=eth]
    

    This is built on a similar idea. The package vignettes are a good place to start for this sort of syntax.

提交回复
热议问题