A hybrid of kableExtra's pack_rows and add_header_above?

Viewed 94

I am combining my ANOVA tables and contrasts tables to save space. I'm using warp and noise data sets in emmeans for a prototype.

library(emmeans)
library(kableExtra)

warp.lm <- lm(breaks ~ wool * tension, data = warpbreaks)
warp.jt <- joint_tests(warp.lm)

warp.ct <- print(contrast(emmeans(warp.lm, ~ tension, infer = c(F,T))), export = T) 

noise.lm <- lm(noise/10 ~ size * type * side, data = auto.noise)
noise.jt <- joint_tests(noise.lm)

noise.ct <- print(contrast(emmeans(noise.lm , ~ size, infer = c(F,T))), export = T) 


### combine 2 ANOVA tables  
anova_1_2 <- rbind(warp.jt, noise.jt) 

### combine 2 contrast tables  
contrast_1_2 <- cbind(warp.ct$summary[,-c(3:5)], noise.ct$summary[,-c(1,3:5)])

### merge ANOVA and contrast tables  

merge_tab <- rbind(as.matrix(anova_1_2 ), as.matrix(contrast_1_2))

### create a kable  

rownames(merge_tab) <- NULL

merge_tab %>%
  kbl(linesep = ""  ,
      caption = "A merged table") %>%
  pack_rows("(A) - warp ANOVA", 1,3) %>%
  pack_rows("(B) - noise ANOVA", 4,10)%>%
 pack_rows("(C) Contrasts", 11, 13) %>%
  add_header_above(c("Contrast" = 1, "Warp" = 2, "Noise" = 2))

My current table looks like this.
enter image description here

The output I'm looking for should look like this. Thanks a lot in advance for your help!

enter image description here

0 Answers
Related