从 R 到 LaTeX 的茎叶法
这里是高中统计老师,很抱歉问了这个简单的问题(或者可能不那么简单)。
我正在运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
大量借鉴 @DWin 提供的解决方案:
您可以使用
capture.output
捕获print
语句的输出,并使用latexTranslate
将其转换为 Latex > 在HMisc
包中:Borrowing heavily from the solution provided by @DWin:
You can capture the output from a
print
statement withcapture.output
and translate it to Latex usinglatexTranslate
in packageHMisc
:我假设您只想将结果导出到乳胶,对吗?
您可能想尝试以下通过测试的 Sweave 代码:
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:
stem
函数返回 NULL。它仅通过打印到控制台设备的副作用起作用。您也可以使用接收器,但我猜这与您的目标并没有很接近
当我通过 Hmisc 函数
latex
运行它时,我得到: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
When I run this through the Hmisc function
latex
I get: