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 = c("KS","MN","AL","FL","CA"))
scores = data.frame(id = c(1,1,1,2,2,3,3,3),
score = c(66,75,78,86,85,76,75,90))
merge(data, scores, by = "id")
semi_join(data, scores, by = "id")
In the "scores" data, there are "id" with multiple observations, where each match gets a row following the join. See ?merge
:
If there is more than one match, all possible matches contribute one row each.
However, I want keep only the row corresponding to the first match from the scores
table.
A semi join would have been nice, but I'm not able to select the score from the right table.
Any suggestions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…