簡體   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