[R] Table Formatting

Marc Schwartz marc_schwartz at comcast.net
Wed Feb 11 22:24:21 CET 2009


Harold,

xtable() does not handle multicol's AFAICS. Short of hand coding this,
which would not be hard, you can use Frank's latex() function in the
Hmisc package.

Something along the lines of the following will get close.

install.packages("Hmisc", dependencies = TRUE)
library(Hmisc)
?latex

# Presuming that the source data is in a dataframe called 'DF'

latex(DF, file = "DF.tex",
      title = "",
      rowname = NULL,
      cgroup = c("", "SNA1", "SNA2", "SNA3"),
      n.cgroup = c(1, 2, 2, 2),
      colheads = c("", "Year", rep(c("TACC", "Catch"), 3)))


This give you the DF.tex TeX file that I have attached and to which I
manually added the preamble and \end{document} directives.

I am also attaching a PDF of the resultant table for easy review.

If you wanted to have the second line only going over the column pairs
rather than all of the columns, replace the line:

  \tabularnewline \cline{1-10}

with:

  \tabularnewline
  \cline{3-4}
  \cline{6-7}
  \cline{9-10}


See the DF2.* files attached. I did not see away to do that in the
function arguments, but perhaps I missed something.

Frank has more information here:

  http://biostat.mc.vanderbilt.edu/twiki/bin/view/Main/StatReport

HTH,

Marc Schwartz


on 02/11/2009 02:30 PM Doran, Harold wrote:
> The xtable package is probably going to be helpful 
> 
>> -----Original Message-----
>> From: r-help-bounces at r-project.org 
>> [mailto:r-help-bounces at r-project.org] On Behalf Of jimdare
>> Sent: Wednesday, February 11, 2009 3:26 PM
>> To: r-help at r-project.org
>> Subject: [R] Table Formatting
>>
>>
>> Dear R-Users
>>
>> I have created the following table in R:
>>
>>      Year      TACC.SNA1  Catch.SNA1  TACC.SNA2   Catch.SNA2  
>>  TACC.SNA3  
>> Catch.SNA3 
>> 111 1985-86      9396       18595            1860             
>> 530           
>> 1486           16727     
>> 112 1986-87      3155       12195            9506            
>> 7067          
>> 4991            2300     
>> 113 1987-88      6913        2074             3740            
>> 3609         
>> 1020            6523     
>> 114 1988-89      3210       15677            2225            
>> 9822         
>> 8188            16154     
>> 115 1989-90      7631       15131            5330           
>> 3784          
>> 3772             3748     
>> 116 1990-91      9988        5316             8693          
>> 11583         
>> 5085             19281     
>> 117 1991-92      4961        1250             5969            
>> 274           
>> 5933            1261     
>> 118 1992-93      9041       13398            1467           
>> 2492          
>> 9140             3616     
>> 119 1993-94      3574        2727             1801           
>> 2425          
>> 6039             18808  
>>
>> I need to format this as shown below so I can export it using 
>> LaTeX and paste it into a publication.  Does anyone know how 
>> I could put the 'SNA#'
>> above the column headings and seperate them via a line break? 
>>  Perhaps there is a package I can download that will let me 
>> do this.  Thanks very much for your help,
>>
>> James
>>
>>                       SNA1                      SNA2          
>>           SNA3
>>                  _____________      _____________     _____________
>>   Year        TACC      Catch      TACC       Catch     TACC  
>>     Catch 
>>
>>  1985-86      9396      18595      1860        530      1486  
>>     16727     
>>  1986-87      3155      12195      9506       7067      4991  
>>      2300     
>>  1987-88      6913       2074      3740       3609      1020  
>>      6523     
>>  1988-89      3210      15677      2225       9822      8188  
>>     16154     
>>  1989-90      7631      15131      5330       3784      3772  
>>      3748     
>>  1990-91      9988       5316      8693      11583      5085  
>>     19281     
>>  1991-92      4961       1250      5969        274      5933  
>>      1261     
>>  1992-93      9041      13398      1467       2492      9140  
>>      3616     
>>  1993-94      3574       2727      1801       2425      6039  
>>     18808 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DF.pdf
Type: application/pdf
Size: 11938 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090211/30c83065/attachment.pdf>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DF2.pdf
Type: application/pdf
Size: 11973 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090211/30c83065/attachment-0001.pdf>


More information about the R-help mailing list