# [R] xtable: custom row.names, move caption to top

Michael Friendly friendly at yorku.ca
Wed Nov 27 22:32:54 CET 2013

Thanks so much, Rainer.
Your detailed example has taught me a lot of what I need to use xtable
more productively, in particular the options for the print() method.

-Michael

On 11/27/2013 12:20 PM, Rainer Schuermann wrote:
> UPDATE:
> Now including the LaTeX math formatting
>
> saxtab <- t( as.data.frame( addmargins( Saxony ) ) )
> rownames( saxtab ) <- c( "Males ($k$)", "Families ($n_k$)" )
> saxtab <- xtable( saxtab, digits = 0,
>       caption = "Number of male children in 6115 Saxony families of size 12",
>       align = "l|rrrrrrrrrrrrrr" )
> print( saxtab, caption.placement = "top", include.colnames = FALSE,
>       hline.after = c( NULL, 0, nrow( saxtab ) ),
>       sanitize.text.function = function(x) { x } )
>
> % latex table generated in R 3.0.2 by xtable 1.7-1 package
> % Wed Nov 27 18:16:47 2013
> \begin{table}[ht]
> \centering
> \caption{Number of male children in 6115 Saxony families of size 12}
> \begin{tabular}{l|rrrrrrrrrrrrrr}
>     \hline
> Males ($k$) & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & Sum \\
>    Families ($n_k$) &    3 &   24 &  104 &  286 &  670 & 1033 & 1343 & 1112 &  829 &  478 &  181 &   45 &    7 & 6115 \\
>     \hline
> \end{tabular}
> \end{table}
>
>
>
>
> On Wednesday 27 November 2013 17:43:30 Rainer Schuermann wrote:
>> You get the cation to the top of the table with
>> print( saxtab, caption.placement = "top" )
>>
>> Formatting the table the way you want can be done like this - I did not manage to carry the LaTeX math formatting for the row names over ($k$ and $n_k$)but the rest should be very much what you want:
>>
>> saxtab <- t( as.data.frame( addmargins( Saxony ) ) )
>> rownames( saxtab ) <- c( "Males (k)", "Families (n_k)" )
>> saxtab <- xtable( saxtab, digits = 0,
>>       caption = "Number of male children in 6115 Saxony families of size 12",
>>       align = "l|rrrrrrrrrrrrrr" )
>> print( saxtab, caption.placement = "top", include.colnames = FALSE,
>>       hline.after = c( NULL, 0, nrow( saxtab ) ) )
>>
>>
>> % latex table generated in R 3.0.2 by xtable 1.7-1 package
>> % Wed Nov 27 17:41:17 2013
>> \begin{table}[ht]
>> \centering
>> \caption{Number of male children in 6115 Saxony families of size 12}
>> \begin{tabular}{l|rrrrrrrrrrrrrr}
>>     \hline
>> Males (k) & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & Sum \\
>>    Families (n\_k) &    3 &   24 &  104 &  286 &  670 & 1033 & 1343 & 1112 &  829 &  478 &  181 &   45 &    7 & 6115 \\
>>     \hline
>> \end{tabular}
>> \end{table}
>>
>>
>>
>>
>> On Wednesday 27 November 2013 09:24:22 Michael Friendly wrote:
>>> With xtable, I'm producing one-way tables from table objects in
>>> horizontal form as shown below.
>>> I'd like to change the labels used for the rows and move the caption to
>>> the top of the table,
>>> as is typically standard for tables.  I can hand-edit, but would prefer
>>> to do it in code.
>>>
>>> data(Saxony, package="vcd")
>>> library(xtable)
>>>       caption="Number of male children in 6115 Saxony families of size 12")
>>>
>>> print(saxtab)
>>>   > print(saxtab)
>>> % latex table generated in R 3.0.1 by xtable 1.7-1 package
>>> % Wed Nov 27 09:12:16 2013
>>> \begin{table}[ht]
>>> \centering
>>> \begin{tabular}{rrrrrrrrrrrrrrr}
>>>     \hline
>>>    & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & Sum \\
>>>     \hline
>>> 1 & 3 & 24 & 104 & 286 & 670 & 1033 & 1343 & 1112 & 829 & 478 & 181 & 45
>>> & 7 & 6115 \\
>>>      \hline
>>> \end{tabular}
>>> \caption{Number of male children in 6115 Saxony families of size 12}
>>> \end{table}
>>>   >
>>>
>>> The desired form looks like this, with row.names = c("Males ($k$)",
>>> "Families ($n_k$)")
>>>
>>> % latex table generated in R 3.0.1 by xtable 1.7-1 package
>>> % Tue Nov 26 14:56:02 2013
>>> \begin{table}[ht]
>>> \caption{Number of male children in 6115 Saxony families of size 12}
>>> \label{tab:saxtab}
>>> \centering
>>> \begin{tabular}{l|rrrrrrrrrrrrrr}
>>>     \hline
>>> Males ($k$) & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & Sum \\
>>>     \hline
>>> Families ($n_k$) & 3 & 24 & 104 & 286 & 670 & 1033 & 1343 & 1112 & 829 &
>>> 478 & 181 & 45 & 7 & 6115 \\
>>>      \hline
>>> \end{tabular}
>>> \end{table}
>>>
>>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> and provide commented, minimal, self-contained, reproducible code.
>

--
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca