Making publication-ready tables with xtable

3 Min Read

When you use R at the command-line, the textual output is limited by the medium: one monospaced font, with no typesetting of any kind. That’s great when you’re doing exploratory analysis, but what about when you want to include R output in a report or publication? In other words, what if you want to convert this Analysis of Variance table:

 
Df Sum Sq Mean Sq F value Pr(>F)
sex 1 75.4 75.37 0.3793 0.539478
ethnicty 3 2572.1 857.38 4.3147 0.006781 **
sex:ethnicty 2 298.4 149.22 0.7509 0.474767
Residuals 93 18480.0 198.71

to this:

 

or this

When you use R at the command-line, the textual output is limited by the medium: one monospaced font, with no typesetting of any kind. That’s great when you’re doing exploratory analysis, but what about when you want to include R output in a report or publication? In other words, what if you want to convert this Analysis of Variance table:

 
Df Sum Sq Mean Sq F value Pr(>F)
sex 1 75.4 75.37 0.3793 0.539478
ethnicty 3 2572.1 857.38 4.3147 0.006781 **
sex:ethnicty 2 298.4 149.22 0.7509 0.474767
Residuals 93 18480.0 198.71

to this:

 

or this:

Df Sum Sq Mean Sq F value Pr(> F)
sex 1 75.37 75.37 0.38 0.5395
ethnicty 3 2572.15 857.38 4.31 0.0068
sex:ethnicty 2 298.43 149.22 0.75 0.4748
Residuals 93 18480.04 198.71

With the xtable package, you can. It’s a handy tool for converting the output of many of R’s statistical functions into a presentation-ready table in LaTeX or HTML format. For example, to create the table above, I simply did the following:

> fm2 <- lm(tlimth ~ sex * ethnicty, data = tli)
> print(xtable(anova(fm2)), type="html")

and then pasted the HTML it generated straight into this blog post. I also tweaked the border=”1″ table directive to border=”0″ — if you have a decent Web editor you could pretty the table up further to your heart’s desire.

You can find more examples of xtable in action in the package vignette.

xtable package: vignette
 

Link to original post

Share This Article
Exit mobile version