r - 如何在R中将两行的值合并在一起
问题描述
我正在尝试将 2 行合并在一起,但似乎找不到方法。
我使用了 geom_col() 并且有两行尼日利亚行。一个标有“尼日利亚”,另一个标有“尼日利亚”。我想将这两行的“sum_gt”值合并为一行。
这是我的代码:
plastics %>%
select(country, year, grand_total) %>%
filter(country != "EMPTY") %>%
drop_na(grand_total) %>%
group_by(country) %>%
summarize(sum_gt = sum(grand_total)) %>%
arrange(desc(sum_gt)) %>%
ggplot(aes(reorder(x = country, sum_gt), y = sum_gt, fill = country)) +
geom_col() +
coord_flip() +
theme(legend.position = "none")
数据集来自 TidyTuesday,这里是 URL:
塑料 <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-01-26/plastics.csv')
解决方案
如果要对它们求和,它们必须具有相同的名称。您的数据集中还有“ECUADOR”和“Ecuador”。为确保您掌握所有情况,我建议您在开始管道之前执行以下操作:
plastics$country<- tolower(plastics$country)
推荐阅读
- python - 从文本文件中检索随机单词
- graphql - 尝试进行更新突变时出现“不正确的填充”错误
- crystal-lang - 如何验证 pem_cert 和签名 (RSA-SHA1)?
- vb.net - 如何在 VB.NET 中将整数和十进制数据类型从 datagridview 保存到数据库 ms 访问
- r - 滚动格兰杰因果检验
- google-chrome-extension - 如何在当前选项卡上运行 chrome 扩展内容脚本
- c# - 属性“主题”已在 values.xml 文件中以不兼容的格式定义
- angular - Firebase AuthStateListener 未能触发功能
- .htaccess - htaccess 重写到可以或不能有 id 的页面
- python - 用前一行值减去当前行中的值并返回较大的值