Select only the first row when merging data frames with multiple matches

后端 未结 4 763
醉梦人生
醉梦人生 2020-12-03 07:52

I have two data frames, \"data\" and \"scores\", and want to merge them on the \"id\" column:

data = data.frame(id = c(1,2,3,4,5),
                  state =          


        
4条回答
  •  执笔经年
    2020-12-03 08:51

    Using data.table along with mult = "first" and nomatch = 0L:

    require(data.table)
    setDT(scores); setDT(data) # convert to data.tables by reference
    
    scores[data, mult = "first", on = "id", nomatch=0L]
    #    id score state
    # 1:  1    66    KS
    # 2:  2    86    MN
    # 3:  3    76    AL
    

    For each row on data's id column, the matching rows in scores' id column are found, and the first one alone is retained (because mult = "first"). If there are no matches, they're removed (because of nomatch = 0L).

提交回复
热议问题