【问题标题】:Stem-and-Leaf from R into LaTeX从 R 到 LaTeX 的 Stem-and-Leaf
【发布时间】:2011-09-07 02:44:34
【问题描述】:

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

我正在运行 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 网站。任何帮助将不胜感激。

【问题讨论】:

  • xtable 可能不会为您执行此操作。我怀疑您将不得不自己在 LaTeX 中创建茎叶图。 tex.stackexachange.com 上的人可能有更好的主意。

标签: r latex


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      stem 函数返回 NULL。它仅通过打印到控制台设备的副作用起作用。

      你也可以使用 sink,但我猜这离你的目​​标不太近

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

      【讨论】:

      • sink 替换为capture.output,然后将latex 替换为latexTranslate,就可以了。看我的回答(你的公然抄袭)。
      • 你的输出看起来不太像 LaTeX-y。看起来更像是当我用 readLines() 读回我的“stem.out”文件时得到的。但我想这取决于迪伦想要什么
      猜你喜欢
      • 2018-11-14
      • 1970-01-01
      • 2016-10-23
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      相关资源
      最近更新 更多