简体   繁体   English

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

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

I am trying to use R studio to combine columns in a matrix of microbial data.我正在尝试使用 R studio 组合微生物数据矩阵中的列。 When I import my .csv into R in looks like:当我将我的 .csv 导入 R 时,如下所示:

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

And I want it to look like:我希望它看起来像:

       species    taxa    another
ind1   4          7       4
ind2   10         2       3

I've seen different codes that allow me to do this on a small scale, but I need something to help me reconcile this for a large data set (54x521).我已经看到不同的代码可以让我在小范围内做到这一点,但我需要一些东西来帮助我协调大数据集(54x521)。

lets say your matrix is called dt, you could do:假设您的矩阵称为 dt,您可以这样做:

Using base R, you could do使用基础 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