load packages and data

insurance_data is sample data provided by the summaryrow package.

library(knitr) # for quick tables
library(summaryrow)

# create reported percentage column
insurance_data$reported_pct <- insurance_data$reported / insurance_data$ultimate

kable(insurance_data)

add a totals summary row

table_1 <- insurance_data %>% totals_row(cols = 2:4)

kable(table_1)

include a totals row label

Additionally use the fill argument to set the fill value supplied in summary row columns not specified in the cols argument. The default is NA.

table_2 <- insurance_data %>% 
             totals_row(cols = 2:4, label_col = 1, label = "Totals:", fill = "")

kable(table_2)

add an averges row

table_3 <- insurance_data %>% 
             averages_row(cols = 5, label_col = 1, label = "Averages:")

kable(table_3)

make a totals and an averages row

table_4 <- insurance_data %>% 
             totals_row(cols = 2:4, label_col = 1, label = "Totals 2 yr:") %>%
             averages_row(cols = 5, label_col = 1, label = "Averages:")

kable(table_4)

make one row with totals and averages

table_5 <- insurance_data %>% 
             totals_row(cols = 2:4) %>%
             averages_row(cols = 5, label_col = 1, label = "Totals/Averages:", 
                          new_row = FALSE)

kable(table_5)

make a totals row including only the last 2 years

table_6 <- insurance_data %>% 
             totals_row(cols = 2:4, label_col = 1, label = "2 Yr Totals:", 
                        rows = 2:3)

kable(table_6)

make two different totals rows

table_7 <- insurance_data %>%
             totals_row(cols = 2:4, label_col = 1, label = "2 Yr Totals:", 
                        rows = 2:3) %>%
             totals_row(cols = 2:4, label_col = 1, label = "Totals:")


kable(table_7)

putting totals and averages together

table_8 <- insurance_data %>% 
             totals_row(cols = 2:4) %>%
             averages_row(cols = 5, label_col = 1, label = "Totals/Averages:", 
                          new_row = FALSE) %>%
             totals_row(cols = 2:4, rows = 2:3) %>%
             averages_row(cols = 5, label_col = 1, label = "2 Yr Totals/Averages:", 
                          rows = 2:3, new_row = FALSE)

kable(table_8)

adding new columns with summary row info

library(dplyr, warn.conflicts = FALSE)
table_9 <- insurance_data %>% 
             totals_row(cols = 2:4, label_col = 1, label = "Totals:") %>%
             averages_row(cols = 5, label_col = 1, label = "Totals/Averages:", 
                          new_row = FALSE) %>%
             dplyr::mutate(loss_cost = ultimate / exposure)

kable(table_9)

now to format those columns

table_9 <- insurance_data %>% 
             totals_row(cols = 2:4, label_col = 1, label = "Totals:") %>%
             averages_row(cols = 5, label_col = 1, label = "Totals/Averages:", 
                          new_row = FALSE)

# add some new columns
table_9$loss_cost <- table_9$ultimate / table_9$exposure
table_9$reported_pct <- table_9$reported / table_9$ultimate

table_9 <- table_9 %>%
             format_numeric(cols = 2, digits = 0) %>%
             format_currency(cols = 3:4, digits = 0) %>%
             format_percent(cols = 5, digits = 2) %>%
             format_numeric(cols = 6, digits = 3) %>%
             format_origin(cols = 1, eval = "2017")

kable(table_9)

adding a blank row

This can be useful for adding some spacing before your summary row

table_10 <- insurance_data %>% 
             blank_row() %>%
             totals_row(cols = 2:4)

kable(table_10)


merlinoa/summaryrow documentation built on May 22, 2019, 6:53 p.m.