Stem and leaf from R to LaTeX

The high school statistics teacher is here, so sorry for the simple question (or maybe not so simple).

I run R to create a plot. I am trying to turn stem and leaf output from stack () into LaTeX. Here is what I got so far:

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

I tried using xtables (because it works for my simple two-way tables):

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

and I get this error:

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

I tried option variations, but I get similar results. From what I can understand, the output from stem () is not a data frame or matrix, so he doesn't like xtables. I tried changing it to a data frame / matrix using as.data.frame () and as.matrix (), but without success. Any help would be greatly appreciated. I ran some google searches without any useful results and looked at the stackoverflow site. Any help would be appreciated.

+3
r latex
source share
3 answers

I assume that you just want to export the result to latex, am I right?

You might want to try the following Sweave code that passed the test:

 \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} 
+3
source share

Borrowing is highly dependent on the solution provided by @DWin:

You can write the output from the print statement using capture.output and translate it into latex using latexTranslate in the HMisc package:

 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] "" 
+5
source share

The stem function returns NULL. It only works through the side effect of printing to a console device.

You can also use the sink, but I guess this is not much closer to your goal.

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

When I run this through the Hmisc latex function, I get:

 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 ------ 
+1
source share

All Articles