将数据。从R转换为Latex,而不需要“行号”(以及booktab)

时间:2021-08-19 21:28:55

I would like to transpose (i.e. vectors as columns) a data.frame in R and export it to Latex without row numbers but with a column instead. I would like to have an output like this:

我想转置(即向量作为列)一个R中的data.frame,并将它导出为Latex而不是行号,而是列。我想有这样的输出:

将数据。从R转换为Latex,而不需要“行号”(以及booktab)

But

df <- data.frame(qwertz=c("a","b","c","d","e","f"), asdfg=c("a","b"))

i.e.

即。

    qwertz asdfg
1      a     a
2      b     b
3      c     a
4      d     b
5      e     a
6      f     b

When I use xtable and booktabs

当我使用xtable和booktabs时

library(xtable)
print(xtable(t(df)), include.colnames=FALSE, booktabs=TRUE)

latex outpout is

乳胶生产是

将数据。从R转换为Latex,而不需要“行号”(以及booktab) i.e.

即。

print(xtable(t(df)), include.colnames=FALSE, booktabs=TRUE)
% latex table generated in R 3.0.2 by xtable 1.7-1 package
\begin{table}[ht]
    \centering
    \begin{tabular}{rllllll}
      \toprule
      \midrule
        qwertz & a & b & c & d & e & f \\ 
         asdfg & a & b & a & b & a & b \\ 
      \bottomrule
    \end{tabular}
\end{table}

Question: how can I have directly the \midrule between the two lines?

问:我怎么能直接在这两行之间使用\midrule ?

I tried with toLatex() from ENmisc and Latex() from Hmisc too and after reading these two questions:

我也尝试了来自ENmisc的toLatex()和来自Hmisc的Latex(),读完这两个问题后:

https://tex.stackexchange.com/q/25575/36408

https://tex.stackexchange.com/q/25575/36408

https://tex.stackexchange.com/q/75793/36408

https://tex.stackexchange.com/q/75793/36408

1 个解决方案

#1


2  

You can use the add.to.row parameter

你可以使用add.to。行参数

library(xtable)
df <- data.frame(qwertz=c("a","b","c","d","e","f"), asdfg=c("a","b"))
df <- t(df)
n <- nrow(df)

print(xtable(df), hline.after = NULL,
      include.colnames = FALSE,
      add.to.row = list(pos = list(0, 1, n),
      command = c('\\toprule[1.5pt]\n', '\\midrule[1pt]\n', '\\bottomrule[1.5pt]\n')))
## % latex table generated in R 3.0.2 by xtable 1.7-1 package
## % Sat Jan 25 14:32:34 2014
## \begin{table}[ht]
## \centering
## \begin{tabular}{rllllll}
##   \toprule[1.5pt]
##  qwertz & a & b & c & d & e & f \\ 
##    \midrule[1pt]
## asdfg & a & b & a & b & a & b \\ 
##    \bottomrule[1.5pt]
## \end{tabular}
## \end{table}

To use it with longtable LaTeX environment you can do something like this

要使用它与长型乳胶环境,您可以这样做

print(xtable(df),
      tabular.environment = "longtable",
      floating = FALSE,
      hline.after = NULL,
      include.colnames = FALSE,
      booktabs = TRUE,
      add.to.row = list(pos = list(0, 1, n),
      command = c('\\toprule[1.5pt]\n', '\\midrule[1pt]\n', '\\bottomrule[1.5pt]\n')))
## % latex table generated in R 3.0.2 by xtable 1.7-1 package
## % Mon Jan 27 13:00:01 2014
## \begin{longtable}{rllllll}
##    \toprule[1.5pt]
## qwertz & a & b & c & d & e & f \\ 
##    \midrule[1pt]
## asdfg & a & b & a & b & a & b \\ 
##    \bottomrule[1.5pt]
## \end{longtable}

#1


2  

You can use the add.to.row parameter

你可以使用add.to。行参数

library(xtable)
df <- data.frame(qwertz=c("a","b","c","d","e","f"), asdfg=c("a","b"))
df <- t(df)
n <- nrow(df)

print(xtable(df), hline.after = NULL,
      include.colnames = FALSE,
      add.to.row = list(pos = list(0, 1, n),
      command = c('\\toprule[1.5pt]\n', '\\midrule[1pt]\n', '\\bottomrule[1.5pt]\n')))
## % latex table generated in R 3.0.2 by xtable 1.7-1 package
## % Sat Jan 25 14:32:34 2014
## \begin{table}[ht]
## \centering
## \begin{tabular}{rllllll}
##   \toprule[1.5pt]
##  qwertz & a & b & c & d & e & f \\ 
##    \midrule[1pt]
## asdfg & a & b & a & b & a & b \\ 
##    \bottomrule[1.5pt]
## \end{tabular}
## \end{table}

To use it with longtable LaTeX environment you can do something like this

要使用它与长型乳胶环境,您可以这样做

print(xtable(df),
      tabular.environment = "longtable",
      floating = FALSE,
      hline.after = NULL,
      include.colnames = FALSE,
      booktabs = TRUE,
      add.to.row = list(pos = list(0, 1, n),
      command = c('\\toprule[1.5pt]\n', '\\midrule[1pt]\n', '\\bottomrule[1.5pt]\n')))
## % latex table generated in R 3.0.2 by xtable 1.7-1 package
## % Mon Jan 27 13:00:01 2014
## \begin{longtable}{rllllll}
##    \toprule[1.5pt]
## qwertz & a & b & c & d & e & f \\ 
##    \midrule[1pt]
## asdfg & a & b & a & b & a & b \\ 
##    \bottomrule[1.5pt]
## \end{longtable}