使用多列作为R中的键进行合并或合并 [英] merge or cbind using multiple columns as key in R
问题描述
我正在使用玩具示例:
Data set A:
color number valueA
red 18 0.2
blue 21 0.6
green 15 0.9
red 10 1.0
blue 11 2.1
green 13 3.6
Data set B:
color number valueB
red 18 0.3
blue 21 0.5
green 15 0.1
red 10 1.1
blue 11 2.5
green 13 3.9
我希望能够合并数据集A和B;我将需要使用颜色和数字来创建唯一键.
我发现的合并和绑定代码似乎使用了一个主键.
有人可以帮我两个或更多主键吗?
谢谢.
I want to be able to merge Dataset A and B; I will need to use color and number to create a unique key.
merge and cbind code that I have found seem to use a single primary key.
Can someone help me with 2 or more primary keys?
Thanks.
推荐答案
merge(data1,data2)
# color number valueA valueB
# 1 blue 11 2.1 2.5
# 2 blue 21 0.6 0.5
# 3 green 13 3.6 3.9
# 4 green 15 0.9 0.1
# 5 red 10 1 1.1
# 6 red 18 0.2 0.3
另请参阅merge
函数的文档.特别是by
,by.x
和by.y
自变量
Please also see documentation of merge
function. Specifically by
, by.x
and by.y
argument
默认情况下,数据框在列上合并,其名称为 两者都有,但可以通过以下方式分别指定各列的规格: by.x 和 by.y..两个数据框中的行与 提取指定的列,并将其连接在一起.如果还有更多 一场比赛之外,所有可能的比赛各贡献一行.为了 匹配"的确切含义,请参见匹配.
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. The rows in the two data frames that match on the specified columns are extracted, and joined together. If there is more than one match, all possible matches contribute one row each. For the precise meaning of ‘match’, see match.
这篇关于使用多列作为R中的键进行合并或合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!