R dplyr左联接-多个返回值和新行:如何仅要求第一个匹配? [英] R dplyr left join - multiple returned values and new rows: how to ask for the first match only?

查看:75
本文介绍了R dplyr左联接-多个返回值和新行:如何仅要求第一个匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我在单独的表格中列出了郊区名称,犯罪率及其理事会名称.

Let's say I have a list of suburb names, crime rate and their council names on a separate table.

我知道left_join(table1,table2,by = Suburb)将返回具有新添加的行的表,这是因为理事会的多个匹配项.问题在于,郊区3和4重叠成两个议会.

I know that left_join(table1, table2, by=Suburb) will return the table with newly added rows due to the multiple matches for council. The problem is that suburbs 3 and 4 overlap into two councils.

有没有办法只获得左联接只返回第一个匹配项,而不是创建新的行来促进多余的行?

Is there a way to only get the left join to only return the first match only rather than creating new rows to facilitate for the extra ones?

此外,在表2上,是否存在仅保留每个郊区的第一行并删除第二/第三/第四委员会重叠的第二/第三/第四实例的功能?

In addition, on Table 2, is there a function to only keep the first row of each suburb and remove the second/third/fourth instances where the second/third/fourth council overlapping occurs?

推荐答案

您可以使用plyr包和join()函数执行此操作.等效于left_join(table1,table2,by = Suburb),但仅使用table2中的第一个Suburb匹配项是:join(table1,table2,by = Suburb,type ="left",match ="first").尽管我很想知道自己,但我不确定dplyr软件包中的等效内容是什么.

You can do this using the plyr package and the join() function. The equivalent to left_join(table1, table2, by=Suburb) but only using the first Suburb match from table2 would be: join(table1, table2, by=Suburb, type="left", match="first"). I'm not sure what the equivalent is in the dplyr package, though I would love to know myself.

这篇关于R dplyr左联接-多个返回值和新行:如何仅要求第一个匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆