如何基于两列组合两个数据框? [英] How do I combine two data-frames based on two columns?

查看:32
本文介绍了如何基于两列组合两个数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用 plyr 和它的朋友来组合数据帧,以及 merge,但到目前为止我不知道如何将两个数据帧与基于 2 列的多列?

I know I can use the plyr and its friends to combine dataframes, and merge as well, but so far I don't know how to merge two dataframes with multiple columns based on 2 columns?

推荐答案

请参阅关于 ?merge 的文档,其中指出:

See the documentation on ?merge, which states:

By default the data frames are merged on the columns with names they both have, 
 but separate specifications of the columns can be given by by.x and by.y.

这清楚地暗示 merge 将合并基于多列的数据帧.从文档中给出的最后一个例子:

This clearly implies that merge will merge data frames based on more than one column. From the final example given in the documentation:

x <- data.frame(k1=c(NA,NA,3,4,5), k2=c(1,NA,NA,4,5), data=1:5)
y <- data.frame(k1=c(NA,2,NA,4,5), k2=c(NA,NA,3,4,5), data=1:5)
merge(x, y, by=c("k1","k2")) # NA's match

此示例旨在演示 incomparables 的使用,但它也说明了使用多列的合并.您还可以使用 by.xby.yxy 中的每一个中指定单独的列.

This example was meant to demonstrate the use of incomparables, but it illustrates merging using multiple columns as well. You can also specify separate columns in each of x and y using by.x and by.y.

这篇关于如何基于两列组合两个数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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