从 R 到 LaTeX 的茎叶法

发布于 2024-12-03 01:46:27 字数 745 浏览 2 评论 0原文

这里是高中统计老师,很抱歉问了这个简单的问题(或者可能不那么简单)。

我正在运行 R 来创建茎叶图。我正在尝试将stem() 的茎叶输出转换为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() 将其更改为数据框/矩阵,但没有成功。任何帮助将不胜感激。我已经进行了一些谷歌搜索,但没有找到有用的结果,并浏览了 stackoverflow 网站。任何帮助将不胜感激。

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

I'm running R to create a stem-and-leaf plot. I'm trying to turn a stem-and-leaf output from stem() into LaTeX. Here is what I've 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've tried to use xtables (because it works for my simple two-way tables) as such:

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've tried variations on the the options, but get similar results. From what I can figure, the output from stem() isn't a data frame or matrix, so xtables doesn't like it. I tried changing it to a data frame/matrix using as.data.frame() and as.matrix() but with no success. Any help would be appreciated. I've run a few google searches with no useful results and looked through the stackoverflow site. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路还长,别太狂 2024-12-10 01:46:27

大量借鉴 @DWin 提供的解决方案:

您可以使用 capture.output 捕获 print 语句的输出,并使用 latexTranslate 将其转换为 Latex > 在 HMisc 包中:

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] ""         

Borrowing heavily from the solution provided by @DWin:

You can capture the output from a print statement with capture.output and translate it to Latex using latexTranslate in package HMisc:

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] ""         
叹倦 2024-12-10 01:46:27

我假设您只想将结果导出到乳胶,对吗?

您可能想尝试以下通过测试的 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}

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

You might want to try the following Sweave code, which 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}
断肠人 2024-12-10 01:46:27

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

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

You could also use sink, but I'm guessing that is not much closer to your goal

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

When I run this through the Hmisc function latex 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 ------
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文