Question

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.

Was it helpful?

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")

OTHER TIPS

One possibility:

dat1 <- data.frame(matrix(c(1,0.25,3,0.75),nrow=2,dimnames=list(c('absolute','relative'),c('GNU','Leo'))))
dat <- data.frame(Freq = rownames(dat1), dat1)

print(
  xtable(
           x = dat
         , caption = "Your Caption"
         , label = "tab:dat"
         , align = paste(paste("l|", paste(rep("r", ncol(dat)-1), collapse=''), sep = ""), "|r", sep = "")
         , digits = c(0, rep(3, ncol(dat)))
         )
  , table.placement = "H"
  , caption.placement = "top"
  , include.rownames = FALSE
  , include.colnames = TRUE
  , size = "normalsize"
  , hline.after = c(-1, 0, nrow(dat))
  )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top