我有一些问题让我的乳胶表中的对齐工作,这是一个例子:
library(Hmisc)
set.seed(1)
x <- matrix(round(runif(6*3)*10, 2), ncol=6)
x[1, 2] <- -round(x[1, 2], 0)
x[3, 3] <- -round(x[1, 2], 1)
x[3, 5] <- round(x[1, 2], 1)
ci1 <- apply(x[,2:3], 1, FUN=function(x){
ret <- paste(min(x), "-", max(x))
return(ret)
})
ci2 <- apply(x[,5:6], 1, FUN=function(x){
ret <- paste(min(x), "-", max(x))
return(ret)
})
x <- cbind(x[,1], ci1, x[,4], ci2)
colnames(x) <- c("A", "interval", "B", "interval")
rownames(x) <- sapply(letters[1:3], FUN=function(x){ return(paste("Var_", x, sep=""))})
latex(x, file="", align="rcrc", rowlabel.just="r",
cgroup = c("First", "Second"),
n.cgroup = c(2, 2),
rgroup=c("Grup A and B", "Group C"),
n.rgroup=c(2, NROW(x) - 2),
ctable=T)
这会产生:
% latex.default(x, file = "", align = "rcrc", rowlabel.just = "r", cgroup = c("First", "Second"), n.cgroup = c(2, 2), rgroup = c("Grup A and B", "Group C"), n.rgroup = c(2, NROW(x) - 2), ctable = T)
%
\ctable[ label=x, pos=!tbp, ]{rllcll} {} {\FL\multicolumn{1}{c}{\bfseries x}&
\multicolumn{2}{c}{\bfseries First}&
\multicolumn{1}{c}{\bfseries }&
\multicolumn{2}{c}{\bfseries Second}
\NN \cline{2-3} \cline{5-6}
\multicolumn{1}{r}{}&\multicolumn{1}{c}{A}&\multicolumn{1}{c}{interval}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{B}&\multicolumn{1}{c}{interval}\NN
\ML
{\bfseries Grup A and B}&&&&&\NN
Var_a&2.66&-9 - 9.45&&0.62&4.98 - 6.87\NN
Var_b&3.72&2.02 - 6.61&&2.06&3.84 - 7.18\NN
\ML
{\bfseries Group C}&&&&&\NN
Var_c&5.73&8.98 - 9&&1.77&-9 - 9.92\NN
\LL
}
错误在于:
\ctable[ label=x, pos=!tbp, ]{rllcll}
我希望 rllcll 为 rrccrc ,但它并没有这样做。我已经尝试了aling和halign参数的所有可能的替代方案。手工做这件事有点烦人......
答案 0 :(得分:12)
这是做到这一点的方法。代码的主要更改是使用col.just = c('r', 'c', 'r', 'c')
而不是align
。此外,您的行名称具有下划线,这将导致latex
抛出错误。因此,使用latexTranslate
来转义下划线并防止错误
latex(x, file="", col.just = strsplit("rcrc", "")[[1]], rowlabel.just="r",
cgroup = c("First", "Second"), n.cgroup = c(2, 2),
rgroup = c("Grup A and B", "Group C"), n.rgroup=c(2, NROW(x) - 2),
booktabs = T, rowname = latexTranslate(rownames(x)))