R:t.Test输出LaTex

时间:2014-02-02 13:07:38

标签: r statistics knitr sweave reproducible-research

我想知道是否有任何功能可以将t.test的输出重定向到LaTeX。像这样的事情

library(xtable)
xtable(t.test(extra ~ group, data = sleep))

2 个答案:

答案 0 :(得分:3)

这就是诀窍。

library(schoRsch)
library(xtable)

T.Test <- t.test(extra ~ group, data = sleep)
xtable(
  t_out(toutput=T.Test, n.equal = TRUE, welch.df.exact = TRUE, welch.n = NA,
    d.corr = TRUE, print = TRUE)
    )



                      Test                            Results
1 Welch Two Sample t-test: t(17.78) = -1.86, p = .079, d = NA
% latex table generated in R 3.1.1 by xtable 1.7-3 package
% Mon Aug 25 21:01:13 2014
\begin{table}[ht]
\centering
\begin{tabular}{rll}
  \hline
 & Test & Results \\ 
  \hline
1 & Welch Two Sample t-test: & t(17.78) = -1.86, p = .079, d = NA \\ 
   \hline
\end{tabular}
\end{table}

答案 1 :(得分:1)

您可以使用带knitr::kable函数的简单S3方法:

as.data.frame.htest <- function(x) {
    x <- unclass(x)
    names <- c("statistic", "estimate", "parameter", "p.value")
    x <- x[names]
    x <- x[!sapply(x, is.null)]
    for (i in seq_along(x)) {
        if (!is.null(names(x[[i]])))
            names(x)[i] <- names(x[[i]])
    }
    as.data.frame(x, stringsAsFactors = FALSE)
}

knitr::kable(t.test(extra ~ group, data = sleep), format = "latex")

注意:有关str(t.test(extra ~ group, data = sleep))类结构的详细信息,请参阅htest