Pergunta

I wonder if there is any function to redirect the output of t.test to LaTeX. Some thing like this

library(xtable)
xtable(t.test(extra ~ group, data = sleep))
Foi útil?

Solução

This does the trick.

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}

Outras dicas

You can use a simple S3-method with knitr::kable function:

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")

Note: see str(t.test(extra ~ group, data = sleep)) for more details about the htest class structure.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top