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
).