How to insert custom values in table using gtsummary or gt

64 Views Asked by At

Need a way to insert custom values into table from another dataframe. Any help is appreciated.

library(dplyr)
library(gtsummary)
set.seed(666)
df1 <- data.frame(type=factor(round(runif(n=5000,min=1,max=5.49),digits=0),levels=c(1,2,3,4,5),labels=c("Apollo","Atens","Atiras","Amor","Unknown")),
                  class=factor(round(runif(n=5000,min=1,max=4.49),digits=0),levels=c(1,2,3,4),labels=c("C-type","S-type","M-type","Unknown")))
#custom values to insert in table
df2 <- data.frame(c1=c('type_apollo','type_atens','type_atiras','type_amor','type_unknown','class_c-type','class_s-type','class_m-type','class_unknown'),
                  c2=c('15%','21%','18%','20%','26%','12%','30%','32%','26%'))

example1.tab <- df1 %>% tbl_summary(label=list(type~"Type",class~"Class"))
example2.tab <- df1 %>% tbl_summary(label=list(type~"Type",class~"Class"))
ex_all.tab <- tbl_merge(list(example1.tab, example2.tab)) %>%
     modify_spanning_header(c(stat_0_1, stat_0_2) ~ NA_character_) %>%
     modify_header(stat_0_1~"Adjusted<br>Calculation",stat_0_2~"Standard<br>Calculation")

Here's the current table:

enter image description here

This is what I want:

enter image description here

0

There are 0 best solutions below