[英]LaTeX table through xtable
I wonder how to get LATEX table using xtable function from the following R code. 我想知道如何从以下R代码使用xtable函数获取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
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? 是否可以获取像这样的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
summary
is producing four tables in a list, so using lapply
like so: summary
将在列表中产生四个表,因此像这样使用lapply
:
mod <- aov(Yield ~ Block + A * B + Error(A * Block), data=aovdat)
lapply(summary(mod), xtable)
will produce a list of LaTeX tables. 将产生一个LaTeX表列表。
A for
loop with print
will produce the LaTeX code only, with the table names in the LaTeX caption. 带
print
的for
循环将仅生成LaTeX代码,并将表名包含在LaTeX标题中。
for (i in 1:4) print(xtable(summary(mod)[[i]], names(summary(mod)[i])))
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
不喜欢列数不同的表。 rbind.fill
in the plyr package will do the job rbind.fill
包中的rbind.fill将完成工作
library(plyr)
aovtab <- do.call(rbind.fill, lapply(1:length(summary(mod)),
function(x) unclass(summary(mod)[[x]])[[1]]))
but we have to manually add the rownames back as data because a) rbind.fill
drops them, and b) they were duplicated anyway, which xtable
wouldn't like. 但是我们必须手动将行名添加回数据中,因为a)
rbind.fill
删除了行名,b)无论如何它们都是重复的,而xtable
希望这样做。
SOV <- unlist(lapply(1:length(summary(mod)),
function(x) rownames(unclass(summary(mod)[[x]])[[1]]))
print(xtable(cbind(SOV, aovtab), include.rownames=F)
After playing with this for a little while, the following seems to produce results in LaTeX form. 玩了一段时间后,以下内容似乎以LaTeX形式产生结果。
xtable(as.data.frame(unlist(coef(anova.mod))))
This gives a LaTeX table of the blocks and their effects, as below. 这给出了块及其效果的LaTeX表,如下所示。
\begin{table}[ht]
\begin{center}
\begin{tabular}{rr}
\hline
& unlist(coef(anova.mod)) \\
\hline
(Intercept).(Intercept) & 291.59 \\
A.A1 & 20.81 \\
Block.Block2 & -7.50 \\
Block.Block3 & 5.50 \\
Block.Block4 & -14.25 \\
Block.Block5 & -0.50 \\
Block.Block6 & 11.25 \\
Block.Block7 & -11.75 \\
Block.Block8 & 24.00 \\
A:Block.B1 & 144.25 \\
Within.B1 & 139.00 \\
Within.A1:B1 & 4.33 \\
\hline
\end{tabular}
\end{center}
\end{table}
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. 通常,xtable更喜欢矩阵或数据框输入,R通常返回列表,因此我通常要做的是提取我感兴趣的部分,然后转换为矩阵或数据框。
Apologies for the ugly TeX, there's no support for it here. 对于丑陋的TeX表示歉意,此处不提供支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.