从R中的ftable对象创建一个乳胶表

Sta*_*t-R 9 latex r xtable knitr

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

 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)
Run Code Online (Sandbox Code Playgroud)

result1看起来像......

      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
Run Code Online (Sandbox Code Playgroud)

我实际上正在使用knitr包写一篇文章.有没有办法在编译我的*.rnw文件时自动将result1转换为latex表?

我试过xtable但得到以下错误...

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

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

print(xtable(ftable2data.frame(result1)))
Run Code Online (Sandbox Code Playgroud)

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

print(xtable(ftable2data.frame(result1)),include.rownames=FALSE)
Run Code Online (Sandbox Code Playgroud)

小智 7

作为替代方案,memisc为ftable对象提供了toLatex方法.

library(memisc)
toLatex(result1)
Run Code Online (Sandbox Code Playgroud)


42-*_*42- 5

方法一:

require(MIfuns)
require(Hmisc)
latex(ftable2data.frame(result1))
Run Code Online (Sandbox Code Playgroud)


use*_*114 5

您可以使用该软件包xtable

library(xtable)
mytable=ftable(mydata)
print(
  xtable(format(mytable)),file="~/Desktop/mytable.tex"
)
Run Code Online (Sandbox Code Playgroud)

我不知道它与给出的其他选项相比如何。