2011-09-07 87 views
3

高中統計老師在這裏,所以很抱歉的簡單問題(或者可能不那麼簡單)。從樹入樹脂莖和葉

我正在運行R來創建一個幹葉圖。我正在嘗試將stem()的stem-and-leaf輸出轉換爲LaTeX。以下是我到目前爲止有:

y<- c(50, 26, 31, 57, 19, 24, 22, 23, 38, 13, 50, 13, 34, 23, 30, 49, 13, 15, 51) 
stem(y) 

我試着使用xtables(因爲它適合我的簡單的雙向表)這樣:

print(xtable(stem(y)), type="latex", latex.environments=c("center"), tabular.environment = "tabular", NA.string = "") 

,我得到這個錯誤:

Error in UseMethod("xtable") : no applicable method for 'xtable' applied to an object of class "NULL" 

我試過各種選項,但得到了類似的結果。從我可以看出,stem()的輸出不是數據框或矩陣,所以xtables不喜歡它。我嘗試使用as.data.frame()和as.matrix()將其更改爲數據框/矩陣,但沒有成功。任何幫助,將不勝感激。我運行了一些沒有有用結果的谷歌搜索,並查看了整個計算器網站。任何幫助,將不勝感激。

+1

'xtable'大概是不會爲你做這個。我懷疑你將不得不自己在LaTeX中創建幹葉片情節。 tex.stackexachange.com上的人可能有更好的主意。 – joran

回答

3

我假設你只是想將結果導出到latex中,對不對?

你可能想嘗試以下Sweave代碼,它通過測試:

\documentclass{article} 

\usepackage{listings} 

\begin{document} 

\begin{lstlisting} 
<<echo=F, results=tex>>= 
y<- c(50, 26, 31, 57, 19, 24, 22, 23, 38, 13, 50, 13, 34, 23, 30, 49, 13, 15, 51) 
stem(y) 
@ 
\end{lstlisting} 

\end{document} 
1

stem函數返回NULL。它只能通過打印到控制檯設備的副作用起作用。

你也可以使用水槽,但我猜,是不是更接近你的目標

sink("stem.out") 
stem(y) 
sink() 

當我通過Hmisc功能latex運行此我得到:

latex(readLines("stem.out") 
#--------file output follows---- 
% latex.default(readLines("stem.out")) 
% 
\begin{table}[!tbp] 
\begin{center} 
\begin{tabular}{l}\hline\hline 
\multicolumn{1}{c}{}\tabularnewline 
\hline 
\tabularnewline 
    The decimal point is 1 digit(s) to the right of the |\tabularnewline 
\tabularnewline 
    1 | 33359\tabularnewline 
    2 | 23346\tabularnewline 
    3 | 0148\tabularnewline 
    4 | 9\tabularnewline 
    5 | 0017\tabularnewline 
\tabularnewline 
\hline 
\end{tabular} 

\end{center} 

\end{table} 
#--------- end of file ------ 
+0

用'capture.output'替換'sink',然後用'latexTranslate'替換'latex',你就在那裏。看到我的回答(公然剽竊你的)。 – Andrie

+0

你的輸出看起來並不是很特別的LaTeX-y。看起來更像是當我用readLines()重新讀入我的「stem.out」文件時得到的。但我想這取決於迪倫想要什麼 –

5

借款嚴重來自@DWin提供的解決方案:

您可以從print語句中捕獲輸出,其中capture.output a第二翻譯它包HMisc使用latexTranslate到乳膠:

library(Hmisc) 
latexTranslate(capture.output(stem(y))) 

[1] ""               
[2] " The decimal point is 1 digit(s) to the right of the $|$" 
[3] ""               
[4] " 1 $|$ 33359"            
[5] " 2 $|$ 23346"            
[6] " 3 $|$ 0148"            
[7] " 4 $|$ 9"             
[8] " 5 $|$ 0017"            
[9] ""