我有一个数据集,用来比较每年的国内销售额和总销售额(国内+国际)。
structure(list(year = c(2012, 2012, 2012, 2012, 2013, 2013, 2013,
2013, 2013, 2014, 2014, 2014, 2014, 2014, 2014, 2015, 2015, 2015,
2015, 2015, 2015, 2015, 2015), market = c("national", "international",
"national", "international", "national", "national", "national",
"international", "national", "national", "international", "national",
"national", "national", "international", "international", "national",
"international", "national", "international", "national", "national",
"national"), amount = c(253, 123, 165, 265, 216, 65, 214, 416,
67, 156, 563, 327, 200, 392, 690, 135, 160, 89, 50, 206, 199,
50, 57)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-23L))
为了创建线图,我创建了数据集的变体,如下所示:
df2<- df |>
group_by(year, market) |>
summarise(amount=sum(amount))
但是,现在我有国内市场和国际市场的总数据,但我想用国际市场的数据替换总数据=国内市场+国际市场。有什么方法可以做到这一点吗?我尝试了几种不同的方法来分组/取消分组,但没有成功。
1条答案
按热度按时间ldfqzlk81#
我不太清楚你在这里找什么,但是像这样的东西会起作用吗?
由reprex package(v2.0.1)于2022年12月30日创建