You are here

Export a Table Created by R to a TeX File

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: 

Comments

Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!

I have already tried sweave. Probably it is an excellent tool, but personally I don't feel any necessity to run R codes inside a LaTeX editor. I mean, why should I run some R code on the middle of a document while I can run them on the very good R editors available,view the output,modify the command(if required),and finally export only the required part of the output to the document very easily!

This method would be ideal, however in my case using the setup doesn't write to the assignment.tex file... Looking at the ?print documentation, I can also not find the file specification. In the setup here it just prints it out onto the console, not into the file.
Any pointers ?

On a related note, I really do not like Sweave, much preferring to keep the R stuff separate from Latex, and thus knowing where any errors come from. There are enough situations to make LaTeX scream wolf because of a missing bracket or a backslash, without the need to add to the situation.

> print(Test, file="assignment.tex", append=T, table.placement = "h",
+ caption.placement="bottom", hline.after=seq(from=-1,to=nrow(Test),by=1))
variable
Region 2007 2008 2009 2010 2011
Asia+Australia 221.17 263.97 314.07 327.91 361.61
Belgium 209.02 213.56 194.27 184.77 187.32
Brazil 73.24 82.06 82.06 93.43 99.16
Canada+Mexico 73.64 76.90 78.18 82.93 88.26
Eastern Europe 129.86 154.82 147.16 153.29 165.69
Europe not counted individually 200.81 221.58 308.97 306.47 306.48
France 1096.85 1069.51 790.72 781.42 771.72
Germany 1336.48 1359.04 1243.01 1296.18 1299.06
Italy 590.27 593.72 528.02 496.20 582.25
Netherlands 269.64 272.73 244.67 236.65 248.59
RoW (Middle East, Africa) 44.03 45.41 57.05 56.95 64.75
SA excl Brazil 26.77 30.01 30.83 35.74 38.11
Scandinavia 214.80 231.56 241.24 239.65 251.05
Spain 764.42 853.36 828.70 745.90 732.98
UK 640.92 652.94 721.61 702.21 704.96
US 408.17 410.95 409.78 417.33 433.56

Dear Christian, extremely sorry for my late response. Your code seems OK, and I am not sure why you are not getting the .tex file with the table. Is there any error message? Which version of R are you using?

Thx for sharing this beautiful piece of help. That is exactly what I needed for my work.
I may add a very useful line if you work with "plm" instead of "lm" objects.
xtable.plm

You are the man!! Thank you.