2016-06-22 36 views
2

我正在嘗試將R的輸出寫入Latex。我期待着與R得到很好的格式化的表格:轉置一張表,從R導出到Latex,使用來自Hmisc包的latex()

raw

當我使用乳膠()函數Hmisc包,我得到這樣的:

enter image description here

我如何轉表,使它成爲2x6而不是6x2?我知道在latex()函數中必須存在一些參數,但我無法弄清楚。

+0

我的問題不是如何在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對象相同尺寸的輸出。 –

+2

您可以調換'summary'對象,'latex'將以您希望的方式構成表格。 'a < - rnorm(1000)''Hmisc :: latex(t(summary(a)))' –

回答

2

摘要返回工具結果奇怪的字符串,在我看來只是簡單的醜陋。以下是獲取類似彙總輸出表中的一種替代方案:

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. :

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} 

這是

enter image description here

入住這裏需要一些化妝更像xtable Tools for making latex tables in R