john.gavin@ubs.com
2004-Jul-15 10:07 UTC
[R] formatting tables with long column names via package:xtable within Sweave
Hi, I use the excellent Sweave tools for writing documents but was wondering how to neatly print a data.frame with long column headings. I cant manage to do this via package:xtable. Typically the labels that I would like use for each column consist of more than one word, but even with just one word, the heading is often wider than the contents of the column. So the number of columns that fits onto a page is limited by the width of the column names. I would like to spread the column names over two or more lines, to reduce that column's width, but xtable doesn't seem to allow for this. For example, the code xtable(data.frame( longColumnHeading = 1, "column heading with spaces" = "b")) produces the following latex. % latex table generated in R 1.9.1 by xtable 1.2-2 package % Thu Jul 15 10:36:24 2004 \begin{table}[ht] \begin{center} \begin{tabular}{rrl} \hline & longColumnHeading & column.heading.with.spaces \\ \hline 1 & 1.00 & b \\ \hline \end{tabular} \end{center} \end{table} I would like to be able to change the line with the column headings & longColumHeading & column.heading.with.spaces \\ to a mulit-line format (with narrower column widths), something like: & long & column \\ & Column & heading \\ & Heading & with \\ & & spaces \\ What is the best way to do this or something similar? I am on XP with R 1.9.1 (using XEmacs and ESS). Regards, John. John Gavin <john.gavin at ubs.com>, Quantitative Risk Models and Statistics, UBS Investment Bank, 6th floor, 100 Liverpool St., London EC2M 2RH, UK. Phone +44 (0) 207 567 4289 Fax +44 (0) 207 568 5352 Visit our website at http://www.ubs.com This message contains confidential information and is intend...{{dropped}}
Jonathan Baron
2004-Jul-15 11:08 UTC
[R] formatting tables with long column names via package:xtable within Sweave
On 07/15/04 11:07, john.gavin at ubs.com wrote:>Hi, > >I use the excellent Sweave tools for writing documents >but was wondering how to neatly print a data.frame >with long column headings. >I cant manage to do this via package:xtable.I think a problem with xtable is this:> xtable(d1,align=c("|l|","c","c","c","c|"))Error in "align<-.xtable"(`*tmp*`, value = switch(1 + is.null(align), : "align" must be containing elements of {"r","l","c"} So the align attribute won't accept p(1cm) either, for example, but this might not be the best solution anyway. It seems just as easy to modify your table after xtable makes it than to modify xtable to allow such things, since xtable would not save you any typing time. You could stick in your long headers using \multicolumn{1}{p(2cm)}{blah blah blah}, or \parbox{}, or just use two rows. (I haven't tested any of this.) Jon -- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~baron R search page: http://finzi.psych.upenn.edu/