Search code examples
rlatexxtable

xtable header manipulation


I am using xtable in R to get LaTeX code for tables. I want to modify the first element of the "header" (first row in LaTeX table) but I only saw an option to add text to the end of a line (using add.to.row in xtable).

The following example should clarify what I want.

Running

xtable(matrix(c(1,0.25,3,0.75),nrow=2,dimnames=list(c('absolute','relative'),c('GNU','Leo'))))

gives the output

% latex table generated in R 2.13.0 by xtable 1.5-6 package
% Tue Jun 19 22:39:49 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
  \hline
 & GNU & Leo \\ 
  \hline
absolute & 1.00 & 3.00 \\ 
  relative & 0.25 & 0.75 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

I would like to get the line

  frequency & GNU & Leo \\ 

instead of the line

  & GNU & Leo \\

but I was unable to accomplish this using xtable. Let me know if you have an idea how to do this. Also, I apologize in advance if this is in the wrong place to ask or if I overlooked an answer or an obvious solution.


Solution

  • Another option is to use the latex function from Hmisc:

    library(Hmisc)
    m <- matrix(c(1,0.25,3,0.75),nrow=2,dimnames=list(c('absolute','relative'),c('GNU','Leo')))
    latex(m,file = "",rowlabel = "frequency")