| 门 | 丰富 |
|---|---|
| 拟杆菌门 | 12 |
| 厚壁菌门 | 4 |
| 变形菌门 | 3 |
| 厚壁菌门 | 21 |
| 厚壁菌门 | 9 |
| 拟杆菌门 | 15 |
| 变形菌门 | 3 |
| 拟杆菌门 | 8 |
| 疣微菌 | 2 |
| 拟杆菌门 | 5 |
我的问题是我在 R 工作室有一张像上面这样的桌子。我想通过在新表中组合 Phylum 列中的相似表并在执行此操作时添加 Abundance 列来编写此表,但我找不到方法。样本输出;
| 门 | 丰富 |
|---|---|
| 厚壁菌门 | 34 |
| 拟杆菌门 | 40 |
| 变形菌门 | 6 |
| 疣微菌 | 2 |
你能帮我找到允许我输出上述内容的代码吗?从现在开始谢谢你。
| 门 | 丰富 |
|---|---|
| 拟杆菌门 | 12 |
| 厚壁菌门 | 4 |
| 变形菌门 | 3 |
| 厚壁菌门 | 21 |
| 厚壁菌门 | 9 |
| 拟杆菌门 | 15 |
| 变形菌门 | 3 |
| 拟杆菌门 | 8 |
| 疣微菌 | 2 |
| 拟杆菌门 | 5 |
我的问题是我在 R 工作室有一张像上面这样的桌子。我想通过在新表中组合 Phylum 列中的相似表并在执行此操作时添加 Abundance 列来编写此表,但我找不到方法。样本输出;
| 门 | 丰富 |
|---|---|
| 厚壁菌门 | 34 |
| 拟杆菌门 | 40 |
| 变形菌门 | 6 |
| 疣微菌 | 2 |
你能帮我找到允许我输出上述内容的代码吗?从现在开始谢谢你。
df <- data.frame(Phylum = c("Bacteroidetes","Firmicutes","Proteobacteria","Firmicutes","Firmicutes","Bacteroidetes","Proteobacteria","Bacteroidetes","Verrucomicrobia","Bacteroidetes"),
Abundance = c(12,4,3,21,9,15,3,8,2,5))
df
Phylum Abundance
1 Bacteroidetes 12
2 Firmicutes 4
3 Proteobacteria 3
4 Firmicutes 21
5 Firmicutes 9
6 Bacteroidetes 15
7 Proteobacteria 3
8 Bacteroidetes 8
9 Verrucomicrobia 2
10 Bacteroidetes 5
aggregate(Abundance~.,df,FUN=sum)
输出:
Phylum Abundance
1 Bacteroidetes 40
2 Firmicutes 34
3 Proteobacteria 6
4 Verrucomicrobia 2
您可以使用 dplyr
library(dplyr)
df <- data.frame(Phylum = c("Bacteroidetes","Firmicutes","Proteobacteria","Firmicutes","Firmicutes","Bacteroidetes","Proteobacteria","Bacteroidetes","Verrucomicrobia","Bacteroidetes"),
Abundance = c(12,4,3,21,9,15,3,8,2,5))
df %>%
group_by(Phylum) %>%
summarize(Abundance = sum(Abundance)) %>%
ungroup()