我有两个数据框
不同的论文年份数据:
author_id distinct_paper_year_count
1 3
2 1
4 1
5 4
作者数据:
author_id paper_id confirmed
1 25733 1
2 47276 1
3 79468 1
4 12856 0
现在我想合并,以便所需的输出如下所示:
author_id paper_id confirmed distinct_paper_year_count
1 25733 1 3
2 47276 1 1
3 79468 1 0
4 12856 0 4
在这我需要author_id
s 存在于表中author_data
成为最终的输出。由于没有数据author_id==3
在distinct_paper_year_count中,值distinct_paper_year_count
最终结果中的列应为零(对于author_id==3
).
通过使用合并我得到
merge(distinct_paper_year_data,author_data,by="author_id")
author_id distinct_paper_year_count paper_id confirmed
1 3 25733 1
2 1 47276 1
4 1 12856 0
如何才能达到预期的输出?
您需要一个外部联接:
merge(distinct_paper_year_data,author_data,by="author_id", all=T)
注意:你会得到NA
对于表不匹配的行,例如 {3,5} 中的author_id。也就是说,如果需要,您可以简单地修改 NA。您还可以使用all.x
or all.y
进行左外连接或右外连接。
最后检查一下data.table
更快的连接(和更多功能)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)