[R] getting names of dimnames of xtabs into xtable latex output

Juliet Hannah juliet.hannah at gmail.com
Thu Aug 18 19:41:04 CEST 2011


Thanks to Duncan Mackay and Dennis Murphy for help.

The following solution seems to give me what I need.

library(memisc)
toLatex(ftable(cyl ~ am,data=mtcars))

For this to work, we have to use:

\documentclass{article}
\usepackage{booktabs}
\usepackage{dcolumn}
\begin{document}

at the beginning of the tex file. I learned this from some of Paul
Johnson's notes I found online ("Getting Nice Latex tables out of R").





On Tue, Aug 16, 2011 at 11:06 PM, Duncan Mackay <mackay at northnet.com.au> wrote:
> Hi
>
> Will this fix the problem?
>
> str(table2)
> xtable(data.frame(table2))
> % latex table generated in R 2.13.1 by xtable 1.5-6 package
> % Wed Aug 17 13:02:38 2011
> \begin{table}[ht]
> \begin{center}
> \begin{tabular}{rllr}
>  \hline
>  & change\_diet & mydiet & Freq \\
>  \hline
> 1 & Don't know & 0 & 26.00 \\
>  2 & Somewhat likely & 0 & 0.00 \\
>  3 & Somewhat unlikely & 0 & 40.00 \\
>  4 & Very likely & 0 & 0.00 \\
>  5 & Very unlikely & 0 & 10.00 \\
>  6 & Don't know & 1 & 0.00 \\
>  7 & Somewhat likely & 1 & 188.00 \\
>  8 & Somewhat unlikely & 1 & 0.00 \\
>  9 & Very likely & 1 & 281.00 \\
>  10 & Very unlikely & 1 & 0.00 \\
>   \hline
> \end{tabular}
> \end{center}
> \end{table}
>
> Regards
>
> Duncan
>
>
> Duncan Mackay
> Department of Agronomy and Soil Science
> University of New England
> ARMIDALE NSW 2351
> Email: home mackay at northnet.com.au
>
>
>
>
>
> At 02:03 17/08/2011, you wrote:
>>
>> In R, the output of xtabs displays the names of the dimnames.  In the
>> example below, these are "change_diet" and "mydiet". Is there a way to
>> have xtable incorporate these names directly into the latex output.
>> Thanks for your help.
>>
>> table2 <- structure(c(26, 0, 40, 0, 10, 0, 188, 0, 281, 0), .Dim = c(5L,
>> 2L), .Dimnames = structure(list(change_diet = c("Don't know",
>> "Somewhat likely", "Somewhat unlikely", "Very likely", "Very unlikely"
>> ), mydiet = c("0", "1")), .Names = c("change_diet", "mydiet"
>> )), class = c("xtabs", "table"))
>> table2
>>
>>
>> library(xtable)
>> xtable(table2)
>>
>>
>>
>> > sessionInfo()
>> R version 2.13.1 (2011-07-08)
>> Platform: i386-pc-mingw32/i386 (32-bit)
>>
>> locale:
>> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
>> States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C
>> [5] LC_TIME=English_United States.1252
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>
>> other attached packages:
>> [1] xtable_1.5-6
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list