具有各种列类型的 xtable
xtable with various column types
我正在尝试在我的 table 上使用 xtable 和各种列类型,包括数字、乳胶命令和字符串。
示例:
a1 <- paste("\multirow{2}{*}{", c(1, 2, 3, 4),"}", sep = "")
a2 <- rep(NA, 4)
riffle <- function (a,b) {
n <- min(length(a), length(b))
p1 <- as.vector(rbind(a[1:n], b[1:n]))
p2 <- c(a[-(1:n)], b[-(1:n)])
c(p1, p2)
}
a <- riffle(a1, a2)
b <- rnorm(8, 0, 1)
c <- rep(c("a", "b"), 2)
d <- rexp(8, 2)*10
tab <- cbind(a, b, c, d)
print(xtable(tab), include.rownames = FALSE)
然而,输出并不令人满意:
\begin{table}[ht]
\centering
\begin{tabular}{llll}
\hline
a & b & c & d \
\hline
$\backslash$multirow\{2\}\{*\}\{1\} & 0.567183110307595 & a & 17.4277735359976 \
& -1.45110043335658 & b & 5.78849371967322 \
$\backslash$multirow\{2\}\{*\}\{2\} & 1.19040399046713 & a & 0.692481031836454 \
& 1.08180700975955 & b & 8.30343133216384 \
$\backslash$multirow\{2\}\{*\}\{3\} & 0.770520985309568 & a & 4.95384422575539 \
& 0.318790423633696 & b & 1.55130311148241 \
$\backslash$multirow\{2\}\{*\}\{4\} & 0.672622845121449 & a & 6.01311508645727 \
& 1.09581061308637 & b & 2.99998417729512 \
\hline
\end{tabular}
\end{table}
我想得到这样的东西:
\begin{table}[ht]
\centering
\begin{tabular}{llll}
\hline
a & b & c & d \
\hline
\multirow{2}{*}{1} & 0.567 & a & 17 \
& -1.451 & b & 6 \
\multirow{2}{*}{2} & 1.190 & a & 1 \
& 1.082 & b & 8 \
\multirow{2}{*}{3} & 0.771 & a & 5 \
& 0.319 & b & 2 \
\multirow{2}{*}{4} & 0.673 & a & 6 \
& 1.096 & b & 3 \
\hline
\end{tabular}
\end{table}
包括数字列的不同舍入(打印时使用 digits
命令通常可行,但不适用于我的 table)
解决方案是在 print()
函数中为 xtable
对象使用 sanitize.text.function = force
。
tab <- data.frame(a, b, c, d)
print(xtable(tab), include.rownames = FALSE, sanitize.text.function = force)
输出:
\begin{table}[ht]
\centering
\begin{tabular}{lrlr}
\hline
a & b & c & d \
\hline
\multirow{2}{*}{1} & 1.26 & a & 2.43 \
& -0.42 & b & 3.60 \
\multirow{2}{*}{2} & -0.14 & a & 6.27 \
& -0.12 & b & 1.68 \
\multirow{2}{*}{3} & 0.70 & a & 2.15 \
& -1.05 & b & 0.62 \
\multirow{2}{*}{4} & 2.29 & a & 0.84 \
& -0.21 & b & 3.36 \
\hline
\end{tabular}
\end{table}
我正在尝试在我的 table 上使用 xtable 和各种列类型,包括数字、乳胶命令和字符串。
示例:
a1 <- paste("\multirow{2}{*}{", c(1, 2, 3, 4),"}", sep = "")
a2 <- rep(NA, 4)
riffle <- function (a,b) {
n <- min(length(a), length(b))
p1 <- as.vector(rbind(a[1:n], b[1:n]))
p2 <- c(a[-(1:n)], b[-(1:n)])
c(p1, p2)
}
a <- riffle(a1, a2)
b <- rnorm(8, 0, 1)
c <- rep(c("a", "b"), 2)
d <- rexp(8, 2)*10
tab <- cbind(a, b, c, d)
print(xtable(tab), include.rownames = FALSE)
然而,输出并不令人满意:
\begin{table}[ht]
\centering
\begin{tabular}{llll}
\hline
a & b & c & d \
\hline
$\backslash$multirow\{2\}\{*\}\{1\} & 0.567183110307595 & a & 17.4277735359976 \
& -1.45110043335658 & b & 5.78849371967322 \
$\backslash$multirow\{2\}\{*\}\{2\} & 1.19040399046713 & a & 0.692481031836454 \
& 1.08180700975955 & b & 8.30343133216384 \
$\backslash$multirow\{2\}\{*\}\{3\} & 0.770520985309568 & a & 4.95384422575539 \
& 0.318790423633696 & b & 1.55130311148241 \
$\backslash$multirow\{2\}\{*\}\{4\} & 0.672622845121449 & a & 6.01311508645727 \
& 1.09581061308637 & b & 2.99998417729512 \
\hline
\end{tabular}
\end{table}
我想得到这样的东西:
\begin{table}[ht]
\centering
\begin{tabular}{llll}
\hline
a & b & c & d \
\hline
\multirow{2}{*}{1} & 0.567 & a & 17 \
& -1.451 & b & 6 \
\multirow{2}{*}{2} & 1.190 & a & 1 \
& 1.082 & b & 8 \
\multirow{2}{*}{3} & 0.771 & a & 5 \
& 0.319 & b & 2 \
\multirow{2}{*}{4} & 0.673 & a & 6 \
& 1.096 & b & 3 \
\hline
\end{tabular}
\end{table}
包括数字列的不同舍入(打印时使用 digits
命令通常可行,但不适用于我的 table)
解决方案是在 print()
函数中为 xtable
对象使用 sanitize.text.function = force
。
tab <- data.frame(a, b, c, d)
print(xtable(tab), include.rownames = FALSE, sanitize.text.function = force)
输出:
\begin{table}[ht]
\centering
\begin{tabular}{lrlr}
\hline
a & b & c & d \
\hline
\multirow{2}{*}{1} & 1.26 & a & 2.43 \
& -0.42 & b & 3.60 \
\multirow{2}{*}{2} & -0.14 & a & 6.27 \
& -0.12 & b & 1.68 \
\multirow{2}{*}{3} & 0.70 & a & 2.15 \
& -1.05 & b & 0.62 \
\multirow{2}{*}{4} & 2.29 & a & 0.84 \
& -0.21 & b & 3.36 \
\hline
\end{tabular}
\end{table}