0

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!

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
intael
  • 508
  • 2
  • 7
  • 21

2 Answers2

2

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}
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • Great! @MichaelChirico But now I'm receiving an error when the table is crossed with another variable... It says: The length of dimnames[1] is not equal to the extension of the array. – intael Aug 17 '15 at 16:28
  • 2
    when you cross with another variable, you should probably use `rownames` and `colnames`. – MichaelChirico Aug 17 '15 at 16:54
  • Thank you for your help! – intael Aug 17 '15 at 16:56
  • Actually, with `table(x,y)`, the row and column names should already be the levels of `x` and `y`, respectively; you only need to use the `dimnames` approach if you want to change the labelling. – MichaelChirico Aug 17 '15 at 17:00
0

You can use dnn inside table and that fix it.

tab <- prop.table(table(conv, dnn = "Whatever you want"))*100
Sociopath
  • 13,068
  • 19
  • 47
  • 75
Frish Vanmol
  • 314
  • 2
  • 6