[英]Problems forming a LaTeX table using xtable-package in R
I would like to form the following LaTeX-table with xtable-package in R.我想在 R 中使用 xtable-package 形成以下 LaTeX 表。
Table 1: Wanted LaTeX table表1:通缉LaTeX表
However, I do not understand how to add multiple extra rows to xtable-function to get above table.但是,我不明白如何向 xtable-function 添加多个额外的行以获取上面的表格。
For example using simple code like this例如使用这样的简单代码
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")
Now, how do you add the the extra rows, alignments, hlines(eg, Country, United States, Method etc.) to xtable-function to the achieve above table?现在,如何将额外的行、对齐方式、hlines(例如,Country、United States、Method 等)添加到 xtable-function 到上表的实现中?
I already looked at https://cran.r-project.org/web/packages/xtable/vignettes/xtableGallery.pdf but I did not find any clear anwer to my question.我已经看过https://cran.r-project.org/web/packages/xtable/vignettes/xtableGallery.pdf但我没有找到任何明确的答案。
Any help would be appreciated!任何帮助,将不胜感激!
Here's my approach (with inspiration from )这是我的方法(灵感来自)
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
can be used to include custom latex code into xtable. add.to.row
可用于将自定义 latex 代码包含到 xtable 中。
I did could not figure out how to place your column names & A & B
after the custom header.我确实不知道如何在自定义 header 之后放置列名& A & B
My solution for this was to use include.colnames = FALSE
and manually inserted column headers in add.to.row
.我的解决方案是使用include.colnames = FALSE
并在add.to.row
中手动插入列标题。 (perhaps someone else has a more elegant solution?) (也许其他人有更优雅的解决方案?)
Result:结果:
% 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.