按关键列合并两个data.frames [英] Merging two data.frames by key column
本文介绍了按关键列合并两个data.frames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数据框。在第一个中,我有一个KEY / ID列和两个变量:
I have two dataframes. In the first one, I have a KEY/ID column and two variables:
KEY V1 V2
1 10 2
2 20 4
3 30 6
4 40 8
5 50 10
在第二个数据帧中,我有一个KEY / ID列和第三个变量
In the second dataframe, I have a KEY/ID column and a third variable
KEY V3
1 5
2 10
3 20
我想通过根据KEY列进行匹配来提取也在第二个数据帧中的第一个数据帧的行。我还想将V3列添加到最终数据集中。
I would like to extract the rows of the first dataframe that are also in the second dataframe by matching them according to the KEY column. I would also like to add the V3 column to final dataset.
KEY V1 V2 V3
1 10 2 5
2 20 4 10
3 30 6 20
这是我尝试使用子集和合并函数
This are my attempts by using the subset and the merge function
subset(data1, data1$KEY == data2$KEY)
merge(data1, data2, by.x = "KEY", by.y = "KEY")
无他们会完成任务。
任何提示都会得到体现。谢谢!
Any hint would be appreaciated. Thank you!
推荐答案
merge(data1,data2,by = KEY)
应该这样做!
这篇关于按关键列合并两个data.frames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文