【问题标题】:Merging various DataFrames from Same Column合并来自同一列的各种 DataFrame
【发布时间】:2020-05-26 17:03:38
【问题描述】:

我正在通过一个公共键列(第一列)将两个数据框合并在一起,但是我想根据前一列的第二列再次添加相同的列:

clusering_matrix_example <- data.frame(BGC = c("BGC1", "BGC2", "BGC3", "BGC4"), Family = c("10","20","30","40"))
network_matrix_example <- data.frame(BGC1 = c("BGC1", "BGC1", "BGC1", "BGC2", "BGC2", "BGC2", "BGC3", "BGC3", "BGC3", "BGC4", "BGC4", "BGC4"),
                                     BGC2 = c("BGC2", "BGC3", "BGC4", "BGC1", "BGC3", "BGC4", "BGC1", "BGC2", "BGC4", "BGC1", "BGC2", "BGC3"),
                                     score = c(1,2,3,1,4,5,2,4,6,3,5,6))
network_output_example <- merge(network_matrix_example, clusering_matrix_example, by.x= "BGC1", by.y = "BGC")

network_output_example <- merge(network_matrix_example, clusering_matrix_example, by.x= "BGC2", by.y = "BGC")

第一个 DF 的输出示例

BGC1  | BGC2 | score |Family
BGC1    BGC2    1     10
BGC1    BGC3    2     10
BGC1    BGC4    3     10
BGC2    BGC1    1     20
BGC2    BGC3    4     20
BGC2    BGC4    5     20
BGC3    BGC1    2     30
BGC3    BGC2    4     30
BGC3    BGC4    6     30
BGC4    BGC1    3     40
BGC4    BGC2    5     40
BGC4    BGC3    6     40

所需的输出 DF

BGC1  | BGC2 | score |Family1 | Family2
BGC1    BGC2    1     10        20
BGC1    BGC3    2     10        30
BGC1    BGC4    3     10        40
BGC2    BGC1    1     20        10
BGC2    BGC3    4     20        30
BGC2    BGC4    5     20        40
BGC3    BGC1    2     30        10
BGC3    BGC2    4     30        20
BGC3    BGC4    6     30        40
BGC4    BGC1    3     40        10
BGC4    BGC2    5     40        20
BGC4    BGC3    6     40        40

这些可能也有不同的长度,所以我会合并 all = TRUE 吗?

【问题讨论】:

  • 我不想替换它,我只想将它再次添加为辅助列,我不知道这是否有意义。就像我会得到第一个数据框,然后添加第二个。

标签: r dataframe merge dplyr


【解决方案1】:

只需在第二次合并中更改名称

clusering_matrix_example <- data.frame(BGC = c("BGC1", "BGC2", "BGC3", "BGC4"), Family = c("10","20","30","40"))

ne1 <- data.frame(BGC1 = c("BGC1", "BGC1", "BGC1", "BGC2", "BGC2", "BGC2", "BGC3", "BGC3", "BGC3", "BGC4", "BGC4", "BGC4"),
                                     BGC2 = c("BGC2", "BGC3", "BGC4", "BGC1", "BGC3", "BGC4", "BGC1", "BGC2", "BGC4", "BGC1", "BGC2", "BGC3"),
                                     score = c(1,2,3,1,4,5,2,4,6,3,5,6))

ne2 <- merge(ne1, clusering_matrix_example, by.x= "BGC1", by.y = "BGC")

network_output_example <- merge(ne2, clusering_matrix_example, by.x= "BGC2", by.y = "BGC")

【讨论】:

  • 如果我希望它们的大小都与 ne1 相同,是否可以添加诸如 all.x = TRUE 之类的参数?
  • 是的,为什么不呢?但实际上 ne1,ne2 和 network_output 的大小都相同(12 obs)。但是对于另一种情况,如果您添加这些参数行将被添加到输出中,即使 x 和 y 之间没有匹配(在 all.x 的情况下),这些情况也会被添加为 NA。这是你的问题吗?
【解决方案2】:
df1 <- merge(network_matrix_example, clusering_matrix_example, by.x= c("BGC1"), by.y = "BGC")

df2 <- merge(df1, clusering_matrix_example, by.x= c("BGC2"), by.y = "BGC")

names(df2)[names(df2)=="Family.x"] <- "Family1"
names(df2)[names(df2)=="Family.y"] <- "Family2"

df3 <- df2[,c("BGC1","BGC2","score","Family1","Family2")]

df4 <- df3[with(df3, order(BGC1,BGC2,score,Family1,Family2)),]

看起来像您想要的输出。

【讨论】:

    猜你喜欢
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2019-06-09
    • 2013-11-13
    • 2016-03-22
    • 1970-01-01
    • 2017-11-11
    相关资源
    最近更新 更多