只是停留在一些df操作上。我有一个很大的ASV矩阵,以样本为行,分类群为列。我想合并特定的行,同时添加这些行的矩阵值。
示例数据帧(代码如下):

我想将sample-1、sample-2和sample-3彼此合并。示例4和示例5也是如此。合并后的数据集将只有两行,这两行包含前一行的值的总和。(具体地说,前三行将成为具有新ASV值的单行: 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"))发布于 2021-11-12 23:10:52
我们可以使用:
library(tidyverse)
df %>%
group_by(group = c(1,1,1,2,2)) %>%
summarize(across(everything(), sum))这就给出了:
# 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 4https://stackoverflow.com/questions/69949927
复制相似问题