我有两个数据框,"数据"和"分数",并希望将它们合并到"id"列:
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")
Run Code Online (Sandbox Code Playgroud)
在"得分"数据中,存在具有多个观察的"id",其中每个匹配在连接之后获得一行.见?merge:
如果有多个匹配,则所有可能的匹配每个贡献一行.
但是,我想只保留与表中第一个匹配对应的行scores.
半连接本来不错,但我无法从右表中选择得分.
有什么建议?