gpt4 book ai didi

r - 合并或 cbind 使用多列作为 R 中的键

转载 作者:行者123 更新时间:2023-12-04 02:41:36 25 4
gpt4 key购买 nike

我正在使用玩具示例:

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;我需要使用颜色和数字来创建一个唯一的 key 。
我发现的合并和 cbind 代码似乎使用单个主键。
有人可以用 2 个或更多主键帮助我吗?谢谢。

最佳答案

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 函数的文档。特别是 byby.xby.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 - 合并或 cbind 使用多列作为 R 中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19802999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com