【问题标题】:How to combine columns with similar names in large matrix如何在大矩阵中组合具有相似名称的列
【发布时间】:2020-06-18 11:54:10
【问题描述】:

我正在尝试使用 R studio 来组合微生物数据矩阵中的列。当我将 .csv 导入 R 时,如下所示:

        species.1   taxa.1   species.2   another.1  taxa.2
ind1    2           4        2           4          3
ind2    4           2        6           3          0

我希望它看起来像:

       species    taxa    another
ind1   4          7       4
ind2   10         2       3

我看到了允许我在小范围内执行此操作的不同代码,但我需要一些东西来帮助我为大型数据集 (54x521) 进行协调。

【问题讨论】:

    标签: r matrix merge


    【解决方案1】:

    假设您的矩阵称为 dt,您可以这样做:

    使用base R,你可以做到

    sapply(split.default(data.frame(dt),sub("\\..*","",colnames(dt))),rowSums)
    
         another species taxa
    ind1       4       4    7
    ind2       3      10    2
    

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      相关资源
      最近更新 更多