Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic R - Outputting basic R correlation table -> LaTex or text

Tags:

r

correlation

I am generating a correlation table with http://myowelt.blogspot.com/2008/04/beautiful-correlation-tables-in-r.html

I am not successful, however, in outputting the file to a usable LaTex file or text file. I have been unsuccessful using sink() to save the data to a text file.

Suppose I am using the following command:

corstarsl(lpp_axis1)

How would I pipe the output to a text file? I've read the documentation on sink and I'm missing a step somewhere. (I open the connection, execute the command, unlink the file and then I find nothing.)

I've also tried using the output from xtable(cortstarsl(lpp_axis1)) in a tex file yet I receive a "element table not found error. I do not know enough about Tex to track the source of the problem.

Suggestions for outputting this data? Other suggestions for creating a correlation table?

like image 755
d-cubed Avatar asked Feb 21 '11 19:02

d-cubed


1 Answers

Using the code from the web page you link to, I get (with the built in airquality data):

> require(Hmisc)
> require(xtable)
> xtable(corstarsl(airquality))
% latex table generated in R 2.12.1 by xtable 1.5-6 package
% Mon Feb 21 20:00:34 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlllll}
  \hline
 & ozone & solar.r & wind & temp & month \\ 
  \hline
ozone &  &  &  &  &  \\ 
  solar.r &  0.35*** &  &  &  &  \\ 
  wind & -0.60*** & -0.06  &  &  &  \\ 
  temp &  0.70*** &  0.28*** & -0.46*** &  &  \\ 
  month &  0.16  & -0.08  & -0.18*  &  0.42*** &  \\ 
  day & -0.01  & -0.15  &  0.03  & -0.13  & -0.01  \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

So the question then is how to get this TeX output to a file. Here capture.output() is one friend:

> capture.output(xtable(corstarsl(airquality)), file = "mytable.tex")

Which outputs the code to file named mytable.tex:

$ cat mytable.tex 
% latex table generated in R 2.12.1 by xtable 1.5-6 package
% Mon Feb 21 20:01:03 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rlllll}
  \hline
 & ozone & solar.r & wind & temp & month \\ 
  \hline
ozone &  &  &  &  &  \\ 
  solar.r &  0.35*** &  &  &  &  \\ 
  wind & -0.60*** & -0.06  &  &  &  \\ 
  temp &  0.70*** &  0.28*** & -0.46*** &  &  \\ 
  month &  0.16  & -0.08  & -0.18*  &  0.42*** &  \\ 
  day & -0.01  & -0.15  &  0.03  & -0.13  & -0.01  \\ 
   \hline
\end{tabular}
\    end{center}
\end{table}

For "plain" delimited text output, perhaps to dump into a word processor or Spreadsheet, try write.table(), eg:

> write.table(corstarsl(airquality), file = "mytable2.txt")

Which results in a file like this:

$ cat mytable2.txt 
"ozone" "solar.r" "wind" "temp" "month"
"ozone" "" "" "" "" ""
"solar.r" " 0.35***" "" "" "" ""
"wind" "-0.60***" "-0.06 " "" "" ""
"temp" " 0.70***" " 0.28***" "-0.46***" "" ""
"month" " 0.16 " "-0.08 " "-0.18* " " 0.42***" ""
"day" "-0.01 " "-0.15 " " 0.03 " "-0.13 " "-0.01 "

You can alter the quoting and delimiter to your heart's content - see ?write.table.

like image 53
Gavin Simpson Avatar answered Nov 08 '22 15:11

Gavin Simpson