Search code examples
rsweavextable

Change variable name in xtable


I want to change the name of the variable I'm tabulating using xtable() in Sweave. I suppose that it's trivial, but I can't find out how to do it. Here's an example: I want to edit "conv" (it is the name of the variable I'm tabulating) and write whatever I want.

The code I'm using to produce it:

<<results=tex,echo=FALSE>>=
tab<-prop.table(table(conv))*100
print(xtable(tab,
             caption="Conversion a Premium (en tanto por ciento)",
             label="table:Conversion",
             digits=2),latex.environments = "center"
      )
@

The result:

enter image description here

Thanks in advance!


Solution

  • I have always found it easiest to pass a matrix to xtable; the options in declaring a matrix include dimnames, which makes it easy to print out whatever you'd like:

    print(xtable(matrix(tab,dimnames=list(names(tab),"Whatever You'd Like")),
                 caption="Conversion a Premium (en tanto por ciento)",
                 label="table:Conversion",
                 digits=2),latex.environments = "center"
    )
    

    Produces:

    % latex table generated in R 3.2.1 by xtable 1.7-4 package
    % Mon Aug 17 12:01:18 2015
    \begin{table}[ht]
    \centering
    \begin{tabular}{rr}
      \hline
     & Whatever You'd Like \\ 
      \hline
    No se Convierte & 99.20 \\ 
      Se Convierte & 0.80 \\ 
       \hline
    \end{tabular}
    \caption{Conversion a Premium (en tanto por ciento)} 
    \label{table:Conversion}
    \end{table}