我想合并2个数据集,当我在R中运行下面的代码时,却没有显示标题,我想看到每个数据集的列标题。
iriss <- head(iris)
iriss1 <- tail(iris)
print(xtable(rbind(iriss,iriss1)))
预期的输出是这样的,我怎么能从R控制台得到这个?
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrl}
\hline
& Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\
\hline
1 & 5.10 & 3.50 & 1.40 & 0.20 & setosa \\
2 & 4.90 & 3.00 & 1.40 & 0.20 & setosa \\
3 & 4.70 & 3.20 & 1.30 & 0.20 & setosa \\
4 & 4.60 & 3.10 & 1.50 & 0.20 & setosa \\
5 & 5.00 & 3.60 & 1.40 & 0.20 & setosa \\
6 & 5.40 & 3.90 & 1.70 & 0.40 & setosa \\
\hline
& Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\
\hline
145 & 6.70 & 3.30 & 5.70 & 2.50 & virginica \\
146 & 6.70 & 3.00 & 5.20 & 2.30 & virginica \\
147 & 6.30 & 2.50 & 5.00 & 1.90 & virginica \\
148 & 6.50 & 3.00 & 5.20 & 2.00 & virginica \\
149 & 6.20 & 3.40 & 5.40 & 2.30 & virginica \\
150 & 5.90 & 3.00 & 5.10 & 1.80 & virginica \\
\hline
\end{tabular}
\end{table}
非常感谢
安 xtable
本质上是一个带有打印方法的数据框架,它显示了在乳胶文档中复制表格的乳胶代码。因此,不可能在乳胶文档中创建一个 xtable
对象的方式进行打印。
然而,它 是 通过捕获控制台的输出并将其连接到一起,可以将所需的输出发送到控制台。这里有一个函数可以做到这一点。
multi_xtable <- function(...)
{
vars <- as.list(match.call(expand.dots = TRUE))[-1]
df_list <- lapply(vars, eval)
num_cols <- sapply(df_list, length)
if (!all(num_cols == num_cols[1]))
stop("All data frames must have equal number of columns")
xtables <- lapply(df_list, function(x) capture.output(xtable::xtable(x)))
if (length(xtables) == 1)
return(xtables[[1]])
header <- xtables[[1]][1:6]
tail <- xtables[[1]][length(xtables[[1]]) + (-1:0)]
xtables <- lapply(xtables, function(x) x[7:(length(x) - 2)])
xtables <- do.call("c", xtables)
cat(header, xtables, tail, sep = "\n")
}
你可以像这样使用它:
multi_xtable(iriss, iriss1)
#> % latex table generated in R 3.6.2 by xtable 1.8-4 package
#> % Wed Apr 29 12:51:24 2020
#> \begin{table}[ht]
#> \centering
#> \begin{tabular}{rrrrrl}
#> \hline
#> & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\
#> \hline
#> 1 & 5.10 & 3.50 & 1.40 & 0.20 & setosa \\
#> 2 & 4.90 & 3.00 & 1.40 & 0.20 & setosa \\
#> 3 & 4.70 & 3.20 & 1.30 & 0.20 & setosa \\
#> 4 & 4.60 & 3.10 & 1.50 & 0.20 & setosa \\
#> 5 & 5.00 & 3.60 & 1.40 & 0.20 & setosa \\
#> 6 & 5.40 & 3.90 & 1.70 & 0.40 & setosa \\
#> \hline
#> & Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species \\
#> \hline
#> 145 & 6.70 & 3.30 & 5.70 & 2.50 & virginica \\
#> 146 & 6.70 & 3.00 & 5.20 & 2.30 & virginica \\
#> 147 & 6.30 & 2.50 & 5.00 & 1.90 & virginica \\
#> 148 & 6.50 & 3.00 & 5.20 & 2.00 & virginica \\
#> 149 & 6.20 & 3.40 & 5.40 & 2.30 & virginica \\
#> 150 & 5.90 & 3.00 & 5.10 & 1.80 & virginica \\
#> \hline
#> \end{tabular}
#> \end{table}