Merging two data.frames by key column

孤者浪人 提交于 2019-12-05 02:14:48

merge(data1, data2, by="KEY") should do it!

If what you want is an inner join, then your attempt should do it. If it doesn't check the formats of Key columns in both the table using class(data1$key).

Apart from these and the merge suggested by Christian, you can use -

library(plyr)
join(data1, data2, by="KEY", type="inner")

or

library(data.table)
setkey(data1, KEY)
setkey(data2, KEY)
data1[,list(data1,data2)]

You could use a dplyr *_join. Given the sample data, both of the following would give the same result:

library(dplyr)
df_merged <- inner_join(data1, data2, by = 'KEY')
df_merged <- right_join(data1, data2, by = 'KEY')

A inner_join returns all rows from df1 where there are matching values in df2, and all columns from df1 and df2.

A right_join returns all rows from df2, and all columns from df1 and df2.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!