Search code examples
rdplyrgt

Using summary_row() values to calculate group percentage with {gt} package?


I am trying to calculate the percentage for an entire group while using the summary_rows() function from the {gt} package. The problem I am encountering is how to create a function that uses summary_rows() values to calculate a percentage rowwise.

Sample df:

df <- tibble(
       division = c("Science", "Science", "Science"),
       department = c("Biology", "Biology", "Biology"),
       course_num = c('101', '201', "301"),
       widthraws = c(1, 2, 3),
       unsucessful = c(0, 0 , 0),
       successfull = c(1, 3, 4),
       total_enrolled = c(2, 5, 7),
       percent_successful = c(.50, .60, .57)
      )

Sample of gt table:

df %>% 
   group_by(division, department) %>% 
   gt() %>% 
   summary_rows(
     groups = TRUE,
     columns = 4:7,
     missing_text = " ",
     fns = list(
       total = ~sum(.)),
   )

What I would want is the total row of the percent_successful column to be .57. Open to other ideas that would help me achieve this too.

enter image description here


Solution

  • Compute the percentage for total outside and add a layer

    library(gt)
    library(dplyr)
    total_success_perc <- with(df, round(sum(successfull)/sum(total_enrolled), 2))
    
    df %>% 
      group_by(division, department) %>% 
      gt() %>%
      summary_rows(
        groups = TRUE,
        columns = 4:7,
        missing_text = " ",
        fns = list(
          total = ~sum(.)),
      ) %>%
      summary_rows(groups = TRUE, columns = 8, missing_text = " ", 
              fns = list(total = ~  c(total_success_perc)))
    

    -output

    enter image description here