Export a Table Created by R to a TeX File

April 17, 2011
By

(This article was first published on stattler.com - R, and kindly contributed to R-bloggers)

Producing tables in LaTeX might be a difficult task as we can not just copy and paste a table in the editor; we have to write all the numbers and other codes. But with the help of xtable package of R it is possible to produce all the necessary codes for producing table in LaTeX and also possible to export the codes in a tex file. Suppose we have produced summary statistics for Air Pollution Data and now want to export it to a tex file as a tabular format. Here are the codes-

library(xtable)
data<-read.table("data_air.txt", header=T)
data<-data[,1:4]
s<-summary(data)
tab<-xtable(s, caption= "summary statistics of air pollution data", 
align=c("|c","|c","|c","|c","|c|"))

The align argument sets the alignment of the columns (c=center aligned). And the vertical lines in this argument denote the positions of the vertical lines in the table; here a vertical line is put after each c and also before the first one to have vertical line between each column and also on both sides of the table. As we put file=”” the output will be displayed on the screen. And the output that means the code for producing the table is-

% latex table generated in R 2.12.0 by xtable 1.5-6 package
% Thu Dec 23 18:13:08 2010
\begin{table}[ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
  \hline
 &      Wind &   Radiation &       CO &       NO \\ 
  \hline
1 & Min.   : 5.00   & Min.   : 30.00   & Min.   :2.000   & Min.   :1.000   \\ 
  2 & 1st Qu.: 6.00   & 1st Qu.: 68.25   & 1st Qu.:4.000   & 1st Qu.:1.000   \\ 
  3 & Median : 8.00   & Median : 76.50   & Median :4.000   & Median :2.000   \\ 
  4 & Mean   : 7.50   & Mean   : 73.86   & Mean   :4.548   & Mean   :2.190   \\ 
  5 & 3rd Qu.: 8.75   & 3rd Qu.: 84.75   & 3rd Qu.:5.000   & 3rd Qu.:3.000   \\ 
  6 & Max.   :10.00   & Max.   :107.00   & Max.   :7.000   & Max.   :5.000   \\ 
   \hline
\end{tabular}
\caption{summary statistics of air pollution data}
\end{center}
\end{table}

Now let we want to put this table to a tex file; for this purpose we need only little modifications of the previous code. The modified code is-

print(tab,file="assignment.tex",append=T,table.placement = "h",
 caption.placement="bottom", hline.after=seq(from=-1,to=nrow(tab),by=1))

Assignment.tex is the tex file where we want the table to appear, append=T insures that the tex file does not lose its previous data, hline.after denotes the rows of the table after which we want horizontal lines.
The LaTeX table will look like the following-
assignment.jpg

Enjoy.

N.B.-The codes have been produced using R 2.12.0 with xtable version 1.5-6. The previous versions might have some differences.

Category: 
Tag: 



To leave a comment for the author, please follow the link and comment on their blog: stattler.com - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)