# xtable

xtable(x)
Convert an R object, such as a matrix, data frame, or linear model, into a LaTeX formatted table using the xtable function from the xtable package.
• x – An R object. See the sample code below to see a list of xtable-friendly objects.
If you aren’t familiar with LaTeX, it is free software for creating professional-looking documents. LaTeX produces far better results than can be achieved in many other softwares (e.g. Microsoft Word), it is incredibly fast since it is basically a text editor with a back-end that separately creates a PDF output, and it is free. Any special structures in LaTeX (e.g. tables) require some mark-up notation, and that’s what xtable is so cool; it makes this aspect a little easier. Download LaTeX: Mac, Windows. Note that installing LaTeX on Windows can sometimes be a bit tedious, so be prepared.

Example. First, all objects eligible for xtable are listed — ignore the "xtable." part in each method listed. Following this list are two examples: (1) A matrix is formatted for LaTeX. (2) The output of a linear model is also formatted for LaTeX.
> library(xtable)
> methods(xtable)
[1] xtable.anova*           xtable.aov*
[3] xtable.aovlist*         xtable.coxph*
[5] xtable.data.frame*      xtable.glm*
[7] xtable.lm*              xtable.matrix*
[9] xtable.prcomp*          xtable.summary.aov*
[11] xtable.summary.aovlist* xtable.summary.glm*
[13] xtable.summary.lm*      xtable.summary.prcomp*
[15] xtable.table*           xtable.ts*
[17] xtable.zoo*

Non-visible functions are asterisked
>
> mat <- matrix(rnorm(8), 4)
> xtable(mat)
% latex table generated in R 2.13.1 by xtable 1.5-6 package
% Sat Feb  4 23:04:22 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
\hline
& 1 & 2 \\
\hline
1 & 1.23 & -1.07 \\
2 & -0.80 & -0.14 \\
3 & -1.08 & -0.60 \\
4 & -0.16 & -2.18 \\
\hline
\end{tabular}
\end{center}
\end{table}
>
> set.seed(5)
> x <- 1:10
> y <- x + rnorm(10)
> g <- lm(y ~ x)
> xtable(g)
% latex table generated in R 2.13.1 by xtable 1.5-6 package
% Sat Feb  4 23:04:22 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
\hline
& Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
\hline
(Intercept) & 0.0452 & 0.6883 & 0.07 & 0.9493 \\
x & 0.9774 & 0.1109 & 8.81 & 0.0000 \\
\hline
\end{tabular}
\end{center}
\end{table}
Tip. New to LaTeX or want to learn? I taught two lectures introducing LaTeX back in grad school, and the slides can be found at the UCLA Statistical Consulting Center website.
Thank you. Thanks to Yered Hammurabi Pita-Juarez for suggesting this great function!