通过 xtable 的 LaTeX 表
我想知道如何使用 xtable 函数从以下 R 代码中获取 LATEX 表。
Block <- gl(8, 4)
A <- factor(c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1,0,1,0,1))
B <- factor(c(0,0,1,1,0,0,1,1,0,1,0,1,1,0,1,0,0,0,1,1,
0,0,1,1,0,0,1,1,0,0,1,1))
C <- factor(c(0,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,0,1,0,1,
1,0,1,0,0,0,1,1,1,1,0,0))
Yield <- c(101, 373, 398, 291, 312, 106, 265, 450, 106, 306, 324, 449,
272, 89, 407, 338, 87, 324, 279, 471, 323, 128, 423, 334,
131, 103, 445, 437, 324, 361, 302, 272)
aovdat <- data.frame(Block, A, B, C, Yield)
summary(aov(Yield~Block+A*B+Error(A*Block), data=aovdat))
Error: A
Df Sum Sq Mean Sq
A 1 3465.3 3465.3
Error: Block
Df Sum Sq Mean Sq
Block 7 4499 642.71
Error: A:Block
Df Sum Sq Mean Sq F value Pr(>F)
B 1 41616 41616 3.5354 0.1091
Residuals 6 70628 11771
Error: Within
Df Sum Sq Mean Sq F value Pr(>F)
B 1 119568 119568 7.3751 0.01673 *
A:B 1 28 28 0.0017 0.96734
Residuals 14 226975 16213
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
使用以下代码
xtable(summary(aov(Yield~Block+A*B+Error(A*Block), data=aovdat)))
产生以下错误
Error in rbind(deparse.level, ...) :
numbers of columns of arguments do not match
是否可以得到这样的 LaTeX 表?
SOV Df Sum Sq Mean Sq F value Pr(>F)
A 1 3465.3 3465.3
Block 7 4499 642.71
B 1 41616 41616 3.5354 0.1091
Residuals 6 70628 11771
B 1 119568 119568 7.3751 0.01673 *
A:B 1 28 28 0.0017 0.96734
Residuals 14 226975 16213
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
I wonder how to get LATEX table using xtable function from the following R code.
Block <- gl(8, 4)
A <- factor(c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,
0,1,0,1,0,1,0,1,0,1,0,1))
B <- factor(c(0,0,1,1,0,0,1,1,0,1,0,1,1,0,1,0,0,0,1,1,
0,0,1,1,0,0,1,1,0,0,1,1))
C <- factor(c(0,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,0,1,0,1,
1,0,1,0,0,0,1,1,1,1,0,0))
Yield <- c(101, 373, 398, 291, 312, 106, 265, 450, 106, 306, 324, 449,
272, 89, 407, 338, 87, 324, 279, 471, 323, 128, 423, 334,
131, 103, 445, 437, 324, 361, 302, 272)
aovdat <- data.frame(Block, A, B, C, Yield)
summary(aov(Yield~Block+A*B+Error(A*Block), data=aovdat))
Error: A
Df Sum Sq Mean Sq
A 1 3465.3 3465.3
Error: Block
Df Sum Sq Mean Sq
Block 7 4499 642.71
Error: A:Block
Df Sum Sq Mean Sq F value Pr(>F)
B 1 41616 41616 3.5354 0.1091
Residuals 6 70628 11771
Error: Within
Df Sum Sq Mean Sq F value Pr(>F)
B 1 119568 119568 7.3751 0.01673 *
A:B 1 28 28 0.0017 0.96734
Residuals 14 226975 16213
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Using the following code
xtable(summary(aov(Yield~Block+A*B+Error(A*Block), data=aovdat)))
producing the following error
Error in rbind(deparse.level, ...) :
numbers of columns of arguments do not match
Is it possible to get a LaTeX table like this?
SOV Df Sum Sq Mean Sq F value Pr(>F)
A 1 3465.3 3465.3
Block 7 4499 642.71
B 1 41616 41616 3.5354 0.1091
Residuals 6 70628 11771
B 1 119568 119568 7.3751 0.01673 *
A:B 1 28 28 0.0017 0.96734
Residuals 14 226975 16213
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
summary
在一个列表中生成四个表,因此使用lapply
像这样:将生成 LaTeX 表的列表。
带有
print
的for
循环将仅生成 LaTeX 代码,并在 LaTeX 标题中包含表名称。如果您希望所有表作为一个表,则需要做一些工作,因为 rbind 不喜欢具有不同列数的表。 plyr 包中的 rbind.fill 可以完成这项工作
,但我们必须手动将行名作为数据添加回来,因为 a) rbind.fill 会删除它们,b) 它们是无论如何都会重复,这是
xtable
不喜欢的。summary
is producing four tables in a list, so usinglapply
like so:will produce a list of LaTeX tables.
A
for
loop withprint
will produce the LaTeX code only, with the table names in the LaTeX caption.If you want all the tables as one, you need to do a bit of work because
rbind
doesn't like tables with different numbers of columns.rbind.fill
in the plyr package will do the jobbut we have to manually add the rownames back as data because a)
rbind.fill
drops them, and b) they were duplicated anyway, whichxtable
wouldn't like.玩了一段时间后,以下似乎产生了 LaTeX 形式的结果。
这给出了块及其效果的 LaTeX 表,如下所示。
这足以满足您的目的吗?
一般来说,xtable更喜欢矩阵或数据帧输入,而R通常返回列表,所以我通常做的是提取我感兴趣的部分,然后转换为矩阵或数据帧。
对丑陋的 TeX 表示歉意,这里不支持它。
After playing with this for a little while, the following seems to produce results in LaTeX form.
This gives a LaTeX table of the blocks and their effects, as below.
Is this sufficient for your purposes?
In general, xtable prefers matrix or dataframe input, and R often returns lists, so what I normally do is extract the parts I am interested in and then convert to a matrix or a dataframe.
Apologies for the ugly TeX, there's no support for it here.