我如何合并/求和给定列中具有相同值的行。然后是直方图。 R studio

问题描述 投票:0回答:1

我正在使用R studio。

我已经过滤了一些数据以创建只有2个变量的新标题。

df1 <- df %>% dplyr::select(Species, Weight)

哪个也给了我一些示例数据

   Species  Weight
1    Dog      7
2    Cat      2
3    Dog      5
4    Dog      4
.     .       .
.     .       .
245  Cat      3
246  Dog      9
247  Cat      2

这是一个数据示例,因为我使用的数据实际上包含25种鱼类。如何将每种物种的权重加在一起,所以每行只有一个物种,如下所示:

   Species  Weight
1    Dog      734
2    Cat      257

然后我想根据直方图中的总重量来绘制物种。

对此的任何帮助也将不胜感激!

非常感谢。

r database sum histogram rows
1个回答
0
投票

我们可以使用

  df %>%
      group_by(Species) %>%
      summarise(Weight = sum(Weight))
© www.soinside.com 2019 - 2024. All rights reserved.