简体   繁体   中英

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. When I import my .csv into R in looks like:

        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).

lets say your matrix is called dt, you could do:

Using base R, you could do

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

     another species taxa
ind1       4       4    7
ind2       3      10    2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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