【问题标题】:Hmisc separate column headings with rowname = NULLHmisc 使用 rowname = NULL 分隔列标题
【发布时间】:2013-11-22 16:38:32
【问题描述】:

我试图让列标题和标题下方的水平线由组分隔。当我执行以下操作时,

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

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

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

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

【问题讨论】:

    标签: r hmisc


    【解决方案1】:

    latex 函数latex.defaultrownames=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
    }
    

    使用 cgroup.cols 作为从 n.cgroup 参数计算的第一列和最后一列

         first.col last.col
    [1,]         1        5
    [2,]         7       12
    

    我认为这是一个错误。所以目前唯一的方法是手动编辑你的tex输出,使用这个函数,你重复你传递给你的乳胶的参数,作为你的tex filen.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))
    }
    

    最终脚本:

    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))
    

    生成正确的格式

    【讨论】:

      【解决方案2】:

      使用我自己的 huxtable 包的一种方法:

      library(huxtable)
      mt_hux <- as_huxtable(mtcars, add_rownames = TRUE, add_colnames = TRUE)
      mt_hux <- insert_column(mt_hux, rep("", nrow(mt_hux)), after = 6)
      mt_hux <- insert_row(mt_hux, c("", "Group 1", "", "", "", "", "", "Group 2", "", "", "", "", ""))
      mt_hux[2, 1] <- "" # get rid of ugly 'row names'
      
      colspan(mt_hux)[1, c(2, 8)] <- c(5, 6)
      align(mt_hux)[1, c(2, 8)] <- "center"
      top_border(mt_hux)[1,] <- 1
      bottom_border(mt_hux)[1, c(2, 8)] <- 1
      bottom_border(mt_hux)[2,] <- 1
      
      # this should be what you want:
      mt_hux
      

      这有一个限制:你不能得到漂亮的双线。但是你可以改变线宽,如果这是一个足够的替代品。你也可以试试pixiedust 包。

      【讨论】:

      • @dash 我在 github 上看过,这看起来很棒。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 2016-02-23
      • 2014-01-04
      • 2018-01-23
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      相关资源
      最近更新 更多