Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to combine rows in a df while adding the matrix values

Just stuck on some df manipulation. I have a large ASV matrix with samples as rows and taxa as columns. I would like to merge specific rows while adding the matrix values of those rows.

Example data frame (code below):

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would like to merge sample-1, sample-2, and sample-3 with each other. Same for sample-4 and sample-5. The merged dataset would have only two rows which contain the summation of the values for each previous row. (Specifically the first three rows would become a single row with new ASV values: ASV1=11, ASV2=14, ASV3=1, ASV4=2, ASV5=8).

> dput(example.matrix)
structure(list(ASV1 = c(8L, 0L, 3L, 6L, 1L), ASV2 = c(1L, 4L, 
9L, 3L, 2L), ASV3 = c(1L, 0L, 0L, 1L, 1L), ASV4 = c(0L, 0L, 2L, 
3L, 0L), ASV5 = c(0L, 7L, 1L, 4L, 0L)), class = "data.frame", row.names = c("sample-1", 
"sample-2", "sample-3", "sample-4", "sample-5"))

>Solution :

We can use:

library(tidyverse)
df %>%
  group_by(group = c(1,1,1,2,2)) %>%
  summarize(across(everything(), sum))

which gives:

# A tibble: 2 x 6
  group  ASV1  ASV2  ASV3  ASV4  ASV5
  <dbl> <int> <int> <int> <int> <int>
1     1    11    14     1     2     8
2     2     7     5     2     3     4
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading