Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request adorn_subtotals #432

Open
annlaurawalker opened this issue Feb 10, 2021 · 4 comments
Open

Feature Request adorn_subtotals #432

annlaurawalker opened this issue Feb 10, 2021 · 4 comments
Labels
seeking comments Users and any interested parties should please weigh in - this is in a discussion phase!

Comments

@annlaurawalker
Copy link

Feature requests

I've used adorn_totals for months now but I'm wondering if it's possible to have an adorn_subtotals function to get subtotals of groups?

@ggrothendieck
Copy link

Subtotals can be done like this; however, one feature that would make it easier would be if the totals excluded total rows. Note that we had to use filter to exclude the junk rows since it would otherwise total the total rows. Also sometimes one wants the totals at the top rather than the bottom of the group. That would also be a nice feature addition.

Data <-
  structure(list(CT = c("1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2"), SCT = c("1", "1", "1", "1", "1", "1", "2", "2", "2", "2", 
"2", "2", "1", "1", "1", "1", "2", "2", "2", "2", "2", "2"), 
    SSCT = c("1", "2", "3", "1", "2", "3", "1", "2", "1", "2", 
    "1", "2", "1", "2", "1", "2", "1", "2", "3", "1", "2", "3"
    ), Category = c(111L, 112L, 113L, 111L, 112L, 113L, 121L, 
    122L, 121L, 122L, 121L, 122L, 211L, 212L, 211L, 212L, 221L, 
    222L, 223L, 221L, 222L, 223L), Y = c(10L, 12L, 15L, 11L, 
    10L, 13L, 21L, 22L, 20L, 25L, 23L, 24L, 31L, 33L, 36L, 39L, 
    32L, 31L, 36L, 41L, 44L, 45L)), row.names = c(NA, -22L), class = "data.frame")

library(dplyr)
library(janitor)

Data %>% 
  group_by(CT, SCT, SSCT) %>%
  summarize(Y = sum(Y), .groups = "drop") %>%
  adorn_totals %>%
  group_by(CT) %>%
  group_modify(~ adorn_totals(.)) %>%
  group_by(CT, SCT) %>%
  group_modify(~ adorn_totals(.)) %>%
  ungroup %>%
  filter(rowSums(across() == "Total") <= 1)

## # A tibble: 17 x 4
##    CT    SCT   SSCT      Y
##    <chr> <chr> <chr> <int>
##  1 1     1     1        21
##  2 1     1     2        22
##  3 1     1     3        28
##  4 1     1     Total    71
##  5 1     2     1        64
##  6 1     2     2        71
##  7 1     2     Total   135
##  8 1     Total -       206
##  9 2     1     1        67
## 10 2     1     2        72
## 11 2     1     Total   139
## 12 2     2     1        73
## 13 2     2     2        75
## 14 2     2     3        81
## 15 2     2     Total   229
## 16 2     Total -       368
## 17 Total -     -       574

@masterofzion
Copy link

I'm interested in this as well. It'd be great if we were able to do subtotals and grand totals by group, though the example here is useful.

@sfirke
Copy link
Owner

sfirke commented Jul 2, 2021

It seems in-scope to me for tabyl and there seems to be enough interest in it. If someone wants to mock up an example for discussion, then create a pull request for it, I'd review.

@sfirke sfirke added the seeking comments Users and any interested parties should please weigh in - this is in a discussion phase! label Jul 2, 2021
@Chanelle280
Copy link

Chanelle280 commented Sep 27, 2021

Hi, I'm also interested in using sub-totals. I'd like to help with a mock up, but would need to know what exactly this would entail as I've never done one.

I find I always use tabyl() combined with kable() and within the kable package it has pack_rows(). I wonder if this is something that could be reproduced for tabyl()?

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
seeking comments Users and any interested parties should please weigh in - this is in a discussion phase!
Projects
None yet
Development

No branches or pull requests

5 participants