![](/img/trans.png)
[英]Adding a horizontal line between the rows in a latex table using R xtable
[英]Problems forming a LaTeX table using xtable-package in R
我想在 R 中使用 xtable-package 形成以下 LaTeX 表。
表1:通緝LaTeX表
但是,我不明白如何向 xtable-function 添加多個額外的行以獲取上面的表格。
例如使用這樣的簡單代碼
financial <- c(1.23, 1.19)
macro <- c(1.50, 1.40)
X <- rbind(financial, macro)
colnames(X) <- c("A","B")
X
A B
financial 1.23 1.19
macro 1.50 1.40
print(xtable(X), type="latex")
現在,如何將額外的行、對齊方式、hlines(例如,Country、United States、Method 等)添加到 xtable-function 到上表的實現中?
我已經看過https://cran.r-project.org/web/packages/xtable/vignettes/xtableGallery.pdf但我沒有找到任何明確的答案。
任何幫助,將不勝感激!
這是我的方法(靈感來自)
library(xtable)
financial <- c(1.23, 1.19)
macro <- c(1.50, 1.40)
X <- rbind(financial, macro)
colnames(X) <- c("A","B")
addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 0
addtorow$pos[[5]] <- 0
addtorow$pos[[6]] <- 0
addtorow$pos[[7]] <- 0
addtorow$command <- c('Country: & United States & \\\\\n',
'\\hline',
'Method: & Regression & \\\\\n',
'\\hline',
'Models: & RMSE Result & \\\\\n',
'\\hline',
'& A & B \\\\\n')
print(xtable(X), add.to.row = addtorow, include.colnames = FALSE )
add.to.row
可用於將自定義 latex 代碼包含到 xtable 中。
我確實不知道如何在自定義 header 之后放置列名& A & B
我的解決方案是使用include.colnames = FALSE
並在add.to.row
中手動插入列標題。 (也許其他人有更優雅的解決方案?)
結果:
% latex table generated in R 4.0.5 by xtable 1.8-4 package
% Mon Jan 17 14:16:51 2022
\begin{table}[ht]
\centering
\begin{tabular}{rrr}
\hline
Country: & United States & \\
\hline Method: & Regression & \\
\hline Models: & RMSE Result & \\
\hline & A & B \\
\hline
financial & 1.23 & 1.19 \\
macro & 1.50 & 1.40 \\
\hline
\end{tabular}
\end{table}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.