我想知道如何从下面的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 使用以下代码
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 发布于 2011-05-22 05:42:37
summary在列表中生成四个表,因此使用lapply如下所示:
mod <- aov(Yield ~ Block + A * B + Error(A * Block), data=aovdat)
lapply(summary(mod), xtable)将生成LaTeX表的列表。
带有for的print循环只生成LaTeX代码,其中的表名在LaTeX标题中。
for (i in 1:4) print(xtable(summary(mod)[[i]], names(summary(mod)[i])))如果希望将所有表作为一个表,则需要做一些工作,因为rbind不喜欢列数不同的表。plyr包中的rbind.fill将完成这项工作。
library(plyr)
aovtab <- do.call(rbind.fill, lapply(1:length(summary(mod)),
function(x) unclass(summary(mod)[[x]])[[1]]))但是我们必须手动地将行名添加回数据,因为( 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)发布于 2011-05-21 11:54:10
经过一段时间的处理后,下面的内容似乎以LaTeX形式产生了结果。
xtable(as.data.frame(unlist(coef(anova.mod))))这给出了块及其效果的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}这对你的目的足够吗?
一般来说,xtable更喜欢矩阵或dataframe输入,而R通常返回列表,所以我通常做的是提取我感兴趣的部分,然后转换成矩阵或数据。
为丑陋的TeX道歉,这里没有对它的支持。
https://stackoverflow.com/questions/6080770
复制相似问题