filling in columns with matching IDs from two dataframes in R

后端 未结 2 1644
刺人心
刺人心 2021-02-09 15:45

I have two dataframes (df1, df2). I want to fill in the AGE and SEX values from df1 to df2 conditioned on having the same ID between the two. I tried several ways using for-loop

2条回答
  •  忘掉有多难
    2021-02-09 16:03

    You could use match with lapply for this. If we iterate [[ with matching on the ID column of each of the original data sets over a vector of names, we can get the desired result.

    nm <- c("AGE", "SEX")
    df2[nm] <- lapply(nm, function(x) df1[[x]][match(df2$ID, df1$ID)])
    df2
    #      ID AGE SEX  Conc
    # 1 90901  39   0  5.00
    # 2 90901  39   0 10.00
    # 3 90901  39   0 15.00
    # 4 90903  40   1 30.00
    # 5 90903  40   1  5.00
    # 6 90902  28   0  2.45
    # 7 90902  28   0 51.00
    # 8 90902  28   0  1.00
    # 9 70905  NA  NA  0.50
    

    Note that this is also quite a bit faster than merge.

提交回复
热议问题