0

This is a toy example, so the numbers are meaningless, but how would I calculate the summary statistics for proportion in the summary_rows row of the table per group?

proportion is a row-wise calculation, so I can't sum/mean/sd/etc. For average proportion, for example, I want to calculate num[average] / items[average]. I can't figure out how to get a custom function to work for fns across groups.

exibble_a <-
  exibble %>%
  mutate(items = runif(8,10,20)) %>%
  group_by(group) %>%
  mutate(proportion = ifelse(is.na(items / num), 0, items / num)) %>%
  ungroup() %>%
  select(-c(fctr, date, time, datetime))

exibble_b = 
  exibble_a %>%
  group_by(group) %>%
  gt(rowname_col = "row", groupname_col = "group") %>%
  fmt_missing(columns = everything()) %>%
  fmt_percent(columns = proportion,
              decimals = 2) %>%
  summary_rows(groups = TRUE,
               columns = c(num,items),
               fns = list(
                 average = ~ mean(.,na.rm=TRUE),
                 total = ~ sum(.,na.rm=TRUE),
                 SD = ~ sd(.,na.rm=TRUE))) %>%
  summary_rows(groups = TRUE,
               columns = proportion,
               fns = list(
                 average = ~ mean(exibble_a$items) / mean(exibble_a$num, 
                                                          na.rm=TRUE)),
               formatter = fmt_percent,
               decimals = 2,
               use_seps = TRUE)

exibble_b
Nigel Stackhouse
  • 481
  • 4
  • 20

0 Answers0