【问题标题】:Transpose a table, exported from R to Latex with latex() from Hmisc package使用 Hmisc 包中的 latex() 转置从 R 导出到 Latex 的表
【发布时间】:2016-10-23 19:52:04
【问题描述】:

我正在尝试将 R 的输出写入 Latex。我希望从 R 中获得格式良好的表格:

当我使用 Hmisc 包中的 latex() 函数时,我得到了这个:

如何转置表格,使其变为 2x6 而不是 6x2?我知道在 latex() 函数中必须存在一些参数,但我无法弄清楚。

【问题讨论】:

  • 我的问题不是如何在 R 控制台中更改“summary(all_vars_zb1[[1]])”输出的外观,而是如何创建该表的 Latex 版本,其尺寸与在 R 控制台中。无论使用 RI 中的哪个表 (summary(all_vars_zb1[[1]]) - 2x6 或 as.matrix(summary(all_vars_zb1[[1]])), 6x2),latex() 函数的输出都是相同的 - 6x2 表.所以我的问题是关于如何使 latex() 函数在 Latex 中产生与 R 对象具有相同尺寸的输出。
  • 您可以转置summary 对象,latex 将按照您希望的方式形成表格。 a <- rnorm(1000)Hmisc::latex(t(summary(a)))

标签: r latex hmisc


【解决方案1】:

您应该提供 MWE :)

我做的是这个

setwd("/myfolder")    
library(Hmisc)
table <- summary(swiss)
table_transpose <- t(table)

latex(table)
latex(table_transpose)

“t”表示转置,适用于矩阵或表格

在运行latex() 后,R 保存了两个 .tex 文件,我必须将它们粘贴到具有合适结构的文件中,否则将无法编译。

这是最终的.tex

\documentclass[12pt,letterpaper,landscape]{article}
\leftmargin=0.25in
\oddsidemargin=0.25in
\textwidth=6.0in
\topmargin=-0.9in
\textheight=9.25in
\usepackage[margin=1in]{geometry}

\begin{document}

\begin{table}[!tbp]
\begin{center}
\begin{tabular}{lllllll}
\hline\hline
\multicolumn{1}{l}{table}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}\tabularnewline
\hline
  Fertility&Min.   :35.00  &1st Qu.:64.70  &Median :70.40  &Mean   :70.14  &3rd Qu.:78.45  &Max.   :92.50  \tabularnewline
 Agriculture&Min.   : 1.20  &1st Qu.:35.90  &Median :54.10  &Mean   :50.66  &3rd Qu.:67.65  &Max.   :89.70  \tabularnewline
 Examination&Min.   : 3.00  &1st Qu.:12.00  &Median :16.00  &Mean   :16.49  &3rd Qu.:22.00  &Max.   :37.00  \tabularnewline
  Education&Min.   : 1.00  &1st Qu.: 6.00  &Median : 8.00  &Mean   :10.98  &3rd Qu.:12.00  &Max.   :53.00  \tabularnewline
   Catholic&Min.   :  2.150  &1st Qu.:  5.195  &Median : 15.140  &Mean   : 41.144  &3rd Qu.: 93.125  &Max.   :100.000  \tabularnewline
Infant.Mortality&Min.   :10.80  &1st Qu.:18.15  &Median :20.00  &Mean   :19.94  &3rd Qu.:21.70  &Max.   :26.60  \tabularnewline
\hline
\end{tabular}\end{center}
\end{table}

\begin{table}[!tbp]
\begin{center}
\begin{tabular}{lllllll}
\hline\hline
\multicolumn{1}{l}{summary}&\multicolumn{1}{c}{  Fertility}&\multicolumn{1}{c}{ Agriculture}&\multicolumn{1}{c}{ Examination}&\multicolumn{1}{c}{  Education}&\multicolumn{1}{c}{   Catholic}&\multicolumn{1}{c}{Infant.Mortality}\tabularnewline
\hline
&Min.   :35.00  &Min.   : 1.20  &Min.   : 3.00  &Min.   : 1.00  &Min.   :  2.150  &Min.   :10.80  \tabularnewline
&1st Qu.:64.70  &1st Qu.:35.90  &1st Qu.:12.00  &1st Qu.: 6.00  &1st Qu.:  5.195  &1st Qu.:18.15  \tabularnewline
&Median :70.40  &Median :54.10  &Median :16.00  &Median : 8.00  &Median : 15.140  &Median :20.00  \tabularnewline
&Mean   :70.14  &Mean   :50.66  &Mean   :16.49  &Mean   :10.98  &Mean   : 41.144  &Mean   :19.94  \tabularnewline
&3rd Qu.:78.45  &3rd Qu.:67.65  &3rd Qu.:22.00  &3rd Qu.:12.00  &3rd Qu.: 93.125  &3rd Qu.:21.70  \tabularnewline
&Max.   :92.50  &Max.   :89.70  &Max.   :37.00  &Max.   :53.00  &Max.   :100.000  &Max.   :26.60  \tabularnewline
\hline
\end{tabular}\end{center}
\end{table}

\end{document}

这是需要化妆的结果

在此处查看更多工具,例如 xtable Tools for making latex tables in R

【讨论】:

    【解决方案2】:

    Summary 返回一个奇怪的字符串,在我看来这简直是丑陋的。以下是在表格中获得类似摘要输出的替代方法:

    summ <- function(x){
        tmp<-quantile(x, c(0,.25,.5,.75,1))
        names(tmp)<-c("Min", "1st Qu.", "Median", "3rd Qu.", "Max")
        return(tmp)}
    

    用函数Sapply数据返回更灵活的data.frame:

    t(sapply(swiss, summ) )
    
                       Min 1st Qu. Median 3rd Qu.   Max
    Fertility        35.00  64.700  70.40  78.450  92.5
    Agriculture       1.20  35.900  54.10  67.650  89.7
    Examination       3.00  12.000  16.00  22.000  37.0
    Education         1.00   6.000   8.00  12.000  53.0
    Catholic          2.15   5.195  15.14  93.125 100.0
    Infant.Mortality 10.80  18.150  20.00  21.700  26.6
    

    使用上面的@pachamaltese 答案,最终汇总表应该没有表中不必要的Min. :

    【讨论】:

      猜你喜欢
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 2013-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多