允许绑定表中的重复名称

Allow duplicated names in binded tables

更新为什么我改变了我的投票。

此代码显示 table 但 R 不编织 pdf。

all_jt %>%
kbl(longtable = T, booktabs = T, 
      caption = "table") %>% 
  remove_column(7) %>%
  add_header_above(c(" " = 2, "Year 1" = 4, "Year 2" = 4)) %>% 
  kable_styling(latex_options = c("repeat_header")) 

Quitting from lines 13-37 (test_table.Rmd) 
Error in remove_column(., 7) : 
  Removing columns was not implemented for latex kables yet

像这里 Remove_Column from a kable table which will be output as latex/pdf 那样切换到 select(-7) 不起作用,因为 R 不喜欢重复的列名。

我在下面有两个方差分析 table,jt_1jt_2,我想合并它们并只为模型项保留 1 列。当我删除重复的列时,R 将 .1 添加到列的第 7、8、9 和 10 个名称的尾部。

library(emmeans)
library(stringr)

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

jt_1 <- print(joint_tests(warp.lm), export = T) %>% as.data.frame() 


jt_2 <- jt_1

all_jt <- cbind(jt_1, jt_2) %>% 
  setNames(gsub("summary.", "", colnames(.))) 

all_jt[,-6]%>% #to remove the duplicated column for model term
 data.frame(check.names = F) %>% 
kbl(longtable = T, booktabs = T, 
    caption = "table") %>% 
  add_header_above(c(" " = 2, "Year 1" = 4, "Year 2" = 4)) %>% 
  kable_styling(latex_options = c("repeat_header")) 

这是我需要的简要说明。

非常感谢。

您可以使用 kableExtra 中的 remove_column 函数而不是 all_jt[,-6] 来删除列,这使得列名唯一。

library(knitr)
library(kableExtra)

all_jt %>%
kbl(longtable = T, booktabs = T, 
      caption = "table") %>% 
  remove_column(7) %>%
  add_header_above(c(" " = 2, "Year 1" = 4, "Year 2" = 4)) %>% 
  kable_styling(latex_options = c("repeat_header")) 

R 不喜欢 data.frames 中的重复列名。如果逐行执行最后一个代码块,您会注意到 all_jt[, -6] 通过添加“.1”后缀使列名唯一。

The/a 解决方案是直接向 kbl 提供列名,例如

all_jt[,-6] %>%
    kbl(longtable = T, booktabs = T, 
        col.names = gsub("\.\d", "", names(.)),
        caption = "table") %>% 
    add_header_above(c(" " = 2, "Year 1" = 4, "Year 2" = 4)) %>% 
    kable_styling(latex_options = c("repeat_header")) 

这会产生