Hmisc使用rowname = NULL分隔列标题

Jak*_*uss 7 r hmisc

我正在尝试将列标题和标题下方的水平线分组.当我执行以下操作时,它可以工作,

library(Hmisc)
data(mtcars)
latex(mtcars, file ='', cgroup = c("Group 1", "Group 2"), n.cgroup = c(5, 6))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是当我尝试删除rownames时,第1组和第2组下的行合并到同一行

library(Hmisc)
data(mtcars)
latex(mtcars, file ='', cgroup = c("Group 1", "Group 2"), n.cgroup = c(5, 6), rowname = NULL)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

有谁知道解决这个问题的方法?

Ced*_*ric 1

Latex 函数latex.default在 时不使用列组参数rownames=NULL。在函数中我们有:

if (length(rowname)) {
    cx <- cbind(rowname, cx)
    col.just <- c(rowlabel.just, col.just)
    if (length(extracolheads)) 
        extracolheads <- c("", extracolheads)
    collabel.just <- c(rowlabel.just, collabel.just)
    if (length(cgroup) == 0L) 
        colheads <- c(rowlabel, colheads)
    else {
        colheads <- c("", colheads)
        cgroup <- c(rowlabel, cgroup)
        rlj <- ifelse(rowlabel.just == "l", "l", "c")
        cgroup.just <- c(rlj, cgroup.just)
        n.cgroup <- c(1, n.cgroup)
        cgroup.cols <- 1 + cgroup.cols
        cline <- paste(sl, "cline{", cgroup.cols[, 1], "-", cgroup.cols[, 
            2], "}", sep = "", collapse = " ")
    }
    nc <- 1 + nc
}
Run Code Online (Sandbox Code Playgroud)

n.cgroup将 cgroup.cols 作为根据参数计算的第一列和最后一列

     first.col last.col
[1,]         1        5
[2,]         7       12
Run Code Online (Sandbox Code Playgroud)

我认为这是一个错误。因此,目前唯一的方法是使用此函数手动编辑 tex 输出,其中您重复传递给乳胶的参数,以获取 and 的tex file名称n.cgroup

change_cline <- function(file,n.cgroup){
  file_to_edit <-readLines(file)
  pos <- grep("cline",file_to_edit) # get the position of cline in the tex file
  cgroup.cols <- matrix(c(1,n.cgroup[2]+1,n.cgroup[1],sum(n.cgroup)+1), nrow=2) 
  file_to_edit[pos]<- paste("\\cline{", cgroup.cols[, 1], "-", cgroup.cols[, 
          2], "}", sep = "", collapse = " ")
  cat(file_to_edit, file = file,sep="\n")
  return(invisible(NULL))
}
Run Code Online (Sandbox Code Playgroud)

最终脚本:

data(mtcars)
la<-latex(mtcars, file ='mtcars2.tex', cgroup = c("Group 1", "Group 2"), n.cgroup = c(5, 6), rowname = NULL)
change_cline(file='mtcars2.tex',n.cgroup = c(5 ,6))
Run Code Online (Sandbox Code Playgroud)

产生正确的格式

正确格式