繁体   English   中英

如何在大矩阵中组合具有相似名称的列

[英]How to combine columns with similar names in large matrix

我正在尝试使用 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)。

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

使用基础 R,你可以做

sapply(split.default(data.frame(dt),sub("\\..*","",colnames(dt))),rowSums)

     another species taxa
ind1       4       4    7
ind2       3      10    2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM