从R中的ftable对象创建一个乳胶表 [英] Creating a latex table from ftable object in R

查看:123
本文介绍了从R中的ftable对象创建一个乳胶表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在提出问题之前,让我创建一些数据.

Let me create some data before I ask my question.

 my.data <- data.frame(A = sample(seq(1,100,by=5),10,replace=TRUE),W = rnorm(10),X =sample(1:10),Y = sample(c("yes", "no"), 10, replace = TRUE),Z=sample(c('a','b','c','d'),10,replace=TRUE))

attach(my.data)

my.d <- xtabs(W~Z+Y+A);my.d
table.data <- ftable(my.d)

result1 <- round(table.data,2)

result1看起来像..

result1 looks like ..

      A     6    11    16    26    71    76    86    91
Z Y                                                    
a no     0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00
 yes    0.00  0.56  0.00  0.00  0.00  0.79  0.00  0.01

b no     0.61  0.00 -0.22  0.14  0.00  0.00 -0.08  1.71
  yes    0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00

c no     0.00  0.00  0.00  0.00 -0.08  0.00  0.00  0.00
  yes    0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00

d no     0.00  0.00  0.00  0.00  1.00  0.00  0.00  0.00
  yes    0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00

我实际上是在使用knitr软件包撰写文章. 有没有一种方法可以将result1转换为乳胶表 * .rnw文件自动生成时自动生成?

I am actually writing an article using knitr package. Is there a way to transform result1 into a latex table automatically when my *.rnw file is complied ?

我尝试使用xtable,但出现以下错误...

I tried with xtable but got the following error...

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

谢谢@DWin和@Yihui.除了latex()之外,我还使用了xtable,如

Thank you @DWin and @Yihui. Apart from latex(), I also used xtable as stated under

print(xtable(ftable2data.frame(result1)))

要删除不必要的行名,我执行了以下操作

To remove unnecessary row names I did the following

print(xtable(ftable2data.frame(result1)),include.rownames=FALSE)

推荐答案

方法1:

require(MIfuns)
require(Hmisc)
latex(ftable2data.frame(result1))

这篇关于从R中的ftable对象创建一个乳胶表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆