2015-02-27 5 views
7

Mam prostą tabelę z nazwami wymiarów, które chcę eksportować z R na lateks. Szukam prostego sposobu na zrobienie tego, który nie wymaga dodatkowej edycji w lateksie. Wygląda na to, że powinno być łatwe, ale znalazłem wiele innych nierozwiązanych pytań na ten temat.Jak wyeksportować tabelę z R na lateks i podać nazwy wymiarów?

Próbowałem użyć polecenia Hmisclatex po this post. Wydaje ! LaTeX Error: Illegal character in array arg., jak stwierdza PO, ale pytanie pozostaje nierozwiązane. This post ma stos opcji tabeli, ale nie widzę, że adresy wymiarów są adresowane.

R-kod

library(Hmisc) 
latex(table(state.division, state.region), rowlabel = "X", collabel = "Y", file = "") 

wyjścia

%latex.default(table(state.division, state.region), rowlabel = "X",  collabel = "Y", file = "")% 
\begin{table}[!tbp] 
\begin{center} 
\begin{tabular}{lrrrr} 
\hline\hline 
\multicolumn{1}{l}{X}&\multicolumn{1}{Y}{Northeast}&\multicolumn{1}{l}{South}&\multicolumn{1}{Y}{North Central}&\multicolumn{1}{l}{West}\tabularnewline 
\hline 
New England&$6$&$0$&$0$&$0$\tabularnewline 
Middle Atlantic&$3$&$0$&$0$&$0$\tabularnewline 
South Atlantic&$0$&$8$&$0$&$0$\tabularnewline 
East South Central&$0$&$4$&$0$&$0$\tabularnewline 
West South Central&$0$&$4$&$0$&$0$\tabularnewline 
East North Central&$0$&$0$&$5$&$0$\tabularnewline 
West North Central&$0$&$0$&$7$&$0$\tabularnewline 
Mountain&$0$&$0$&$0$&$8$\tabularnewline 
Pacific&$0$&$0$&$0$&$5$\tabularnewline 
\hline 
\end{tabular}\end{center} 

\end{table} 

Komunikaty o błędach Po Latex Rendering

Errors: 

./test.tex:9: LaTeX Error: Illegal character in array arg. [...lumn{1}{l}{X}&\multicolumn{1}{Y}{Northeast}] 
./test.tex:9: LaTeX Error: Illegal character in array arg. [...l}{South}&\multicolumn{1}{Y}{North Central}] 

Adresowanie błędów przez dodanie wyrównania &\multicolumn{1} daje tabelę, która nie ma nazw wymiarów w odpowiednim miejscu.

Pożądany Wyjście

    state.region 
_____________________________________________________________ 
state.division  Northeast South North Central West 
_____________________________________________________________ 
    New England    6  0    0 0 
    Middle Atlantic   3  0    0 0 
    South Atlantic    0  8    0 0 
    East South Central   0  4    0 0 
    West South Central   0  4    0 0 
    East North Central   0  0    5 0 
    West North Central   0  0    7 0 
    Mountain     0  0    0 8 
    Pacific     0  0    0 5 

próbował również przy użyciu memisc package, który podobnie daje Illegal character błędów.

+0

http://stackoverflow.com/questions/12800784/how-to-get-dimnames-in-xtable- table-output może pomóc przy alt: 'library (tables); tblr <- tabular (state.division ~ state.region); lateks (tblr) ' – user20650

+1

@ user20650, który jest przydatny link, dziękuję. – Minnow

Odpowiedz

6

Można użyć do tego xtable (z pakietu xtable):

# create your table 
tab <- table(state.division, state.region) 

# reassemble to put things where they need to be 
tab2 <- cbind(rownames(tab), tab) 
tab3 <- rbind(c("","\\multicolumn{4}{l}{state.region}", rep("",ncol(tab2)-2)), 
       c("state.division",colnames(tab2)[-1]), 
       tab2) 

# print as xtable 
library("xtable") 
print(xtable(tab3), include.rownames = FALSE, include.colnames = FALSE, sanitize.text.function = I, hline.after = c(0,1,2,11)) 

Można użyć file argumentu print.xtable jeśli chcesz pisać to bezpośrednio do pliku. Oto otrzymany kod LaTeX:

% latex table generated in R 3.1.3 by xtable 1.7-4 package 
% Thu Apr 23 21:25:44 2015 
\begin{table}[ht] 
\centering 
\begin{tabular}{lllll} 
    \hline 
& \multicolumn{4}{l}{state.region} & & & \\ 
    \hline 
state.division & Northeast & South & North Central & West \\ 
    \hline 
New England & 6 & 0 & 0 & 0 \\ 
    Middle Atlantic & 3 & 0 & 0 & 0 \\ 
    South Atlantic & 0 & 8 & 0 & 0 \\ 
    East South Central & 0 & 4 & 0 & 0 \\ 
    West South Central & 0 & 4 & 0 & 0 \\ 
    East North Central & 0 & 0 & 5 & 0 \\ 
    West North Central & 0 & 0 & 7 & 0 \\ 
    Mountain & 0 & 0 & 0 & 8 \\ 
    Pacific & 0 & 0 & 0 & 5 \\ 
    \hline 
\end{tabular} 
\end{table} 

A wynik PDF:

enter image description here

Powiązane problemy