我有一个包含3列的数据集:第一列是城市名称,第二列是日期,第三列是水质数据。我已经按照城市名称和日期对我的数据进行了排序,现在我正在尝试将每个城市的水质数据分别求和。你知道我怎么在演播室里做到吗?
任何帮助都将不胜感激。谢谢
发布于 2021-02-03 11:01:44
如果您使用的是tidyverse包,则可以按城市分组,然后汇总信息,并将来自water_quality的所有数据相加
library(tidyverse)
# im using this data set
ds <- tibble(city = "a", wq = 1) %>%
add_row( city = "a", wq = 1) %>%
add_row( city = c("a", "b", "c", "a", "b"), wq = c(.5, .2, .5, .7, 1.2))
#you're interested in this part
ds %>% group_by(city) %>%
summarise(sum = sum(wq))这是输出
# A tibble: 3 x 2
city sum
<chr> <dbl>
1 a 3.2
2 b 1.4
3 c 0.5https://stackoverflow.com/questions/66020501
复制相似问题