我有两个数据帧
distinct_paper_year_data:
author_id distinct_paper_year_count
1 3
2 1
4 1
5 4
Run Code Online (Sandbox Code Playgroud)
author_data:
author_id paper_id confirmed
1 25733 1
2 47276 1
3 79468 1
4 12856 0
Run Code Online (Sandbox Code Playgroud)
现在我想合并,以便所需的输出如下所示:
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
Run Code Online (Sandbox Code Playgroud)
在这里我需要author_id表中的s author_data在最终输出中.由于author_id==3在distinct_paper_year_count中没有数据,因此distinct_paper_year_count在最终结果(for 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
Run Code Online (Sandbox Code Playgroud)
如何获得所需的输出?
Mic*_*ele 10
你需要一个外部联接:
merge(distinct_paper_year_data,author_data,by="author_id", all=T)
Run Code Online (Sandbox Code Playgroud)
注意:您将获得NA表格不匹配的行,例如{3,5}中的author_id.也就是说,如果需要,您可以简单地修改NA.您还可以使用all.x或all.y执行左外连接或右外连接.
最后检查data.table更快的连接(以及更多功能)