使用group_by创建合计和小计

31moq8wy  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(152)

我有一个数据集,用来比较每年的国内销售额和总销售额(国内+国际)。

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))

但是,现在我有国内市场和国际市场的总数据,但我想用国际市场的数据替换总数据=国内市场+国际市场。有什么方法可以做到这一点吗?我尝试了几种不同的方法来分组/取消分组,但没有成功。

ldfqzlk8

ldfqzlk81#

我不太清楚你在这里找什么,但是像这样的东西会起作用吗?

library(dplyr)
sum_dat <- dat %>%
  group_by(market, year) %>% 
  summarise(amount = sum(amount))
#> `summarise()` has grouped output by 'market'. You can override using the
#> `.groups` argument.

alldat <- dat %>% group_by(year) %>% 
  summarise(amount = sum(amount)) %>% 
  mutate(market = "total") %>% 
  dplyr::select(all_of(names(sum_dat))) %>% 
  bind_rows(sum_dat, .)
alldat
#> # A tibble: 12 × 3
#> # Groups:   market [3]
#>    market         year amount
#>    <chr>         <dbl>  <dbl>
#>  1 international  2012    388
#>  2 international  2013    416
#>  3 international  2014   1253
#>  4 international  2015    430
#>  5 national       2012    418
#>  6 national       2013    562
#>  7 national       2014   1075
#>  8 national       2015    516
#>  9 total          2012    806
#> 10 total          2013    978
#> 11 total          2014   2328
#> 12 total          2015    946

reprex package(v2.0.1)于2022年12月30日创建

相关问题