Let's say I have this data:
df <- tibble(
col_a = paste("type",sample(letters[1:2],100,replac=T),sep="_"),
col_b= paste("category",sample(letters[10:12],100,replac=T),sep="_"),
col_c=runif(100, 5.0, 7.5),
col_d=runif(100, 100.0, 105.5)
)
I need to create a summary data table for the total sample as well as grouped by column_a, that looks more or less like this mockup:
Initially, I was going to create each piece separately and then merge it. I'm not sure whether this is the right approach, and I'm not sure if there's a way to merge the GT tables besides using some html tricks , which I prefer to avoid.
Here are the splitted gt-table elements:
table_type_a <- df %>%
filter(col_a=="type_a") %>%
group_by(col_b) %>%
dplyr::summarise(col_c_sum=sum(col_c),
col_d_mean=mean(col_d)) %>%
gt() %>%
tab_spanner(label="interesting numbers",
columns =c(col_c_sum,col_d_mean)) %>%
fmt_number( columns =c(col_c_sum,col_d_mean),
decimals = 1)
table_type_b <- df %>%
filter(col_a=="type_b") %>%
group_by(col_b) %>%
dplyr::summarise(col_c_sum=sum(col_c),
col_d_mean=mean(col_d)) %>%
gt() %>%
tab_spanner(label="interesting numbers",
columns =c(col_c_sum,col_d_mean)) %>%
fmt_number( columns =c(col_c_sum,col_d_mean),
decimals = 1)
Thank you