在调用xtable()
之前,您可以使用正则表达式将\phantom{}
插入到表格中之前:
重新创建您的数据:
x <- structure(list(X = structure(c(1L, 2L, 4L, 3L, 5L), .Label = c("(Intercept)",
"SEXFemale", "SEXFemale:ASIAN", "SEXFemale:BLACK", "SEXFemale:HISPANIC"
), class = "factor"), B.1 = structure(c(3L, 2L, 1L, 1L, 1L), .Label = c("",
"-0.46 (0.023)", "-1.669 (0.093)"), class = "factor"), B.2 = structure(c(3L,
2L, 1L, 1L, 1L), .Label = c("", "-0.386 (0.04)", "-1.701 (0.094)"
), class = "factor"), B.3 = structure(c(4L, 3L, 5L, 1L, 2L), .Label = c("-0.063 (0.089)",
"-0.128 (0.074)", "-0.274 (0.17)", "-1.774 (0.121)", "0.132 (0.163)"
), class = "factor")), .Names = c("X", "B.1", "B.2", "B.3"), class = "data.frame", row.names = c(NA,
-5L))
正则表达式:
x$X <- gsub("SEXFemale:", "\\\\phantom{Female} X ", x$X)
x$X <- gsub("SEXFemale", "Female", x$X)
library(xtable)
xx <- print(xtable(x), print.results = FALSE, include.rownames = FALSE,
sanitize.text.function=function(x)x)
cat(xx)
生成的文本:
\begin{table}[ht]
\begin{center}
\begin{tabular}{llll}
\hline
X & B.1 & B.2 & B.3 \\
\hline
(Intercept) & -1.669 (0.093) & -1.701 (0.094) & -1.774 (0.121) \\
Female & -0.46 (0.023) & -0.386 (0.04) & -0.274 (0.17) \\
\phantom{Female} X BLACK & & & 0.132 (0.163) \\
\phantom{Female} X ASIAN & & & -0.063 (0.089) \\
\phantom{Female} X HISPANIC & & & -0.128 (0.074) \\
\hline
\end{tabular}
\end{center}
\end{table}
最终输出结果如下:
print.xtable(..., print.results = FALSE)
实现,然后使用正则表达式查找第二列并插入\phantom{}
调用来完成。我现在得走了,但是我以后可能会发布一个解决方案。 - Roman Luštrikftable
生成表格,并使用memisc::toLatex
创建它的Latex输出? - ROLO