【问题标题】:How to join 2 or more data frames如何连接 2 个或更多数据框
【发布时间】:2021-06-24 18:55:40
【问题描述】:

我有多个数据框,类似于

DF1

Element Category              Subcategory  Subsystem    Role                             Fcount
E100    Cofactors, Vitamins   Biotin       synthesis    phosphoribosyltransferase domain    1
E100    Cofactors, Vitamins   Biotin       synthesis    Biotin operon repressor             1
E100    Cofactors, Vitamins   Biotin       synthesis    aminotransferase (EC 2.6.1.62)      2
E100    Cofactors, Vitamins   Biotin       synthesis    ligase (EC 6.3.4.15)                1
E100    Cofactors, Vitamins   Biotin       synthesis    synthase (EC 2.3.1.47)              1
E100    Cofactors, Vitamins   Pigments     Biotin       synthase (EC 2.3.1.47)              1

DF2

Element Category              Subcategory  Subsystem    Role                             Fcount
E200    Cofactors, Vitamins   Biotin       synthesis    phosphoribosyltransferase domain    1
E200    Cofactors, Vitamins   Biotin       synthesis    Biotin operon repressor             1
E200    Cofactors, Vitamins   Biotin       synthesis    aminotransferase (EC 2.6.1.62)      2
E200    Cofactors, Vitamins   Biotin       synthesis    toxin (EC 6.3.4.15)                 1
E200    Cofactors, Vitamins   Biotin       synthesis    synthase (EC 2.3.1.47)              1

首先,从 CategoryRole 列中呈现相同字符串的所有行都将被视为相等;如果在一列或多列中不同,则将其视为不同行!!!!

示例 1,DF1 和 DF2 中的第 1 行

这两行在 CategorySubcategorySubsystemRole 列中呈现相同的模式:辅因子、维生素 生物素合成磷酸核糖转移酶结构域,所以算是平等的!!!

示例 2

存在于部分而非所有列中的那些(如:Cofactors, Vitamins Biotin synthesis ligase (EC 6.3.4.15)Cofactors, Vitamins Biotin synthesis toxin (EC 6.3.4.15) )) 在那些不存在的地方加零。对于在SubcategorySubsystem的列中存在差异的DF1的最后一行(Cofactors, Vitamins Pigments Biotin synthase (EC 2.3.1.47),则必须将其添加为不同的行。

Fcount列将改为Element的名称,并从5到N列添加(本例为5到6列)!

最终的数据框会是这样的:

Category              Subcategory  Subsystem    Role                             E100  E200
Cofactors, Vitamins   Biotin       synthesis    phosphoribosyltransferase domain    1   1
Cofactors, Vitamins   Biotin       synthesis    Biotin operon repressor             1   1
Cofactors, Vitamins   Biotin       synthesis    aminotransferase (EC 2.6.1.62)      2   2
Cofactors, Vitamins   Biotin       synthesis    ligase (EC 6.3.4.15)                1   0
Cofactors, Vitamins   Biotin       synthesis    synthase (EC 2.3.1.47)              1   1
Cofactors, Vitamins   Biotin       synthesis    toxin (EC 6.3.4.15)                 0   1
Cofactors, Vitamins   Pigments     Biotin       synthase (EC 2.3.1.47)              1   0

在这种情况下,我只是使用了 2 个数据帧作为示例,但我有超过 20 个。

如何在 R 中做到这一点??

对不起,如果我没有给出任何代码,但我不知道该怎么做!!!

谢谢

【问题讨论】:

    标签: r dataframe merge


    【解决方案1】:

    有几种方法可以做到这一点,但我倾向于使用 tidyverse 解决方案Link to documentation,您只需要使用by 参数即可。

    df_com <- full_join(df1, df2, by = c("Category", "Subcategory", "Subsystem", "Role"))
    

    然后,您可以使用rename 将 Fcount.x 和 Fcount.y 列重命名为 E100 和 E200。 还有一些基本的 R 和 data.table 解决方案,但我对它们不太熟悉

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      相关资源
      最近更新 更多