intael
intael

Reputation: 518

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!

Upvotes: 0

Views: 1630

Answers (2)

Frish Vanmol
Frish Vanmol

Reputation: 354

You can use dnn inside table and that fix it.

tab <- prop.table(table(conv, dnn = "Whatever you want"))*100

Upvotes: 0

MichaelChirico
MichaelChirico

Reputation: 34703

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}

Upvotes: 2

Related Questions