Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

Creating a user-defined total with grand_summary_rows()

$
0
0

I have a table that looks like this:

  category family       amount
  <chr>    <chr>        <chr> 
1 SALES    ONLINE SALES 47    
2 SALES    IN STORE     72    
3 COGS     LABOR        28    
4 COGS     TAXES        35    
5 COGS     WORKERS COMP 24    
6 COGS     BENEFITS     33    
7 EXPENSE  AUTOMOBILE   44    
8 EXPENSE  RENT         12    
9 EXPENSE  TELEPHONE    26    

I am trying to create a gt table from this so I have created this code:

library(tidyverse)
library(gt)

category <- c(rep("SALES",2),
              rep("COGS", 4),
              rep("EXPENSE",3)
)
family <- c("ONLINE SALES","IN STORE","LABOR","TAXES","WORKERS COMP","BENEFITS",
            "AUTOMOBILE", "RENT","TELEPHONE")

amount <- c(47,72,28,35,24,33,44,12,26)


output <- as_tibble(cbind(category,family,amount)) %>% 
    mutate(amount= as.numeric(amount)) %>% 
    gt(rowname_col = 'family',
       groupname_col = 'category') %>% 
    row_group_order(c("SALES","COGS", "EXPENSE")) %>% 
    summary_rows(groups = TRUE,
                 columns = 'amount',
                 fns = list(
                     Total = ~sum(.,na.rm = TRUE)
                 ))
output

How do you get the overall total of SALES-COGS-EXPENSE using the grand_summary_rows() function while all of the amounts are still positive?


Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>