0% found this document useful (0 votes)
77 views

The Xtable Gallery: With Small Contributions From Others November 6, 2009

The document provides examples of tables that can be generated using the xtable package in R to produce LaTeX output. It includes: 1) A data frame table with 10 rows of student data. 2) A matrix table with a model matrix. 3) Tables from aov objects including an analysis of variance. 4) Tables from lm objects including a linear regression. The document serves as a gallery and regression test of different types of tables that can be produced in LaTeX from R objects using the xtable package.

Uploaded by

gpbai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

The Xtable Gallery: With Small Contributions From Others November 6, 2009

The document provides examples of tables that can be generated using the xtable package in R to produce LaTeX output. It includes: 1) A data frame table with 10 rows of student data. 2) A matrix table with a model matrix. 3) Tables from aov objects including an analysis of variance. 4) Tables from lm objects including a linear regression. The document serves as a gallery and regression test of different types of tables that can be produced in LaTeX from R objects using the xtable package.

Uploaded by

gpbai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

The xtable gallery

Jonathan Swinton <[email protected]>


with small contributions from others
November 6, 2009

1 Summary
This document gives a gallery of tables which can be made by using the xtable
package to create LATEX output. It doubles as a regression check for the package.

> library(xtable)

2 Gallery
2.1 Data frame
Load example dataset
> data(tli)
> tli.table <- xtable(tli[1:10, ])
> digits(tli.table)[c(2, 6)] <- 0

> print(tli.table, floating = FALSE)

grade sex disadvg ethnicty tlimth


1 6 M YES HISPANIC 43
2 7 M NO BLACK 88
3 5 F YES HISPANIC 34
4 3 M YES HISPANIC 65
5 8 M YES WHITE 75
6 5 M NO BLACK 74
7 8 F YES HISPANIC 72
8 4 M YES BLACK 79
9 6 M NO WHITE 88
10 7 M YES HISPANIC 87

1
2.2 Matrix
> design.matrix <- model.matrix(~sex * grade, data = tli[1:10,
+ ])
> design.table <- xtable(design.matrix)
> print(design.table, floating = FALSE)
(Intercept) sexM grade sexM:grade
1 1.00 1.00 6.00 6.00
2 1.00 1.00 7.00 7.00
3 1.00 0.00 5.00 0.00
4 1.00 1.00 3.00 3.00
5 1.00 1.00 8.00 8.00
6 1.00 1.00 5.00 5.00
7 1.00 0.00 8.00 0.00
8 1.00 1.00 4.00 4.00
9 1.00 1.00 6.00 6.00
10 1.00 1.00 7.00 7.00

2.3 aov
> fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli)
> fm1.table <- xtable(fm1)
> print(fm1.table, floating = FALSE)
Df Sum Sq Mean Sq F value Pr(>F)
sex 1 75.37 75.37 0.38 0.5417
ethnicty 3 2572.15 857.38 4.27 0.0072
grade 1 36.31 36.31 0.18 0.6717
disadvg 1 59.30 59.30 0.30 0.5882
Residuals 93 18682.87 200.89

2.4 lm
> fm2 <- lm(tlimth ~ sex * ethnicty, data = tli)
> fm2.table <- xtable(fm2)
> print(fm2.table, floating = FALSE)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 73.6364 4.2502 17.33 0.0000
sexM -1.6364 5.8842 -0.28 0.7816
ethnictyHISPANIC -9.7614 6.5501 -1.49 0.1395
ethnictyOTHER 15.8636 10.8360 1.46 0.1466
ethnictyWHITE 4.7970 4.9687 0.97 0.3368
sexM:ethnictyHISPANIC 10.6780 8.7190 1.22 0.2238
sexM:ethnictyWHITE 5.1230 7.0140 0.73 0.4670

2
2.4.1 anova object
> print(xtable(anova(fm2)), floating = FALSE)

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

2.4.2 Another anova object


> fm2b <- lm(tlimth ~ ethnicty, data = tli)

> print(xtable(anova(fm2b, fm2)), floating = FALSE)

Res.Df RSS Df Sum of Sq F Pr(>F)


1 96 19053.59
2 93 18480.04 3 573.55 0.96 0.4141

2.5 glm
> fm3 <- glm(disadvg ~ ethnicty * grade, data = tli, family = binomial())
> fm3.table <- xtable(fm3)

> print(fm3.table, floating = FALSE)

Estimate Std. Error z value Pr(>|z|)


(Intercept) 3.1888 1.5966 2.00 0.0458
ethnictyHISPANIC -0.2848 2.4808 -0.11 0.9086
ethnictyOTHER 212.1701 22122.7093 0.01 0.9923
ethnictyWHITE -8.8150 3.3355 -2.64 0.0082
grade -0.5308 0.2892 -1.84 0.0665
ethnictyHISPANIC:grade 0.2448 0.4357 0.56 0.5742
ethnictyOTHER:grade -32.6014 3393.4687 -0.01 0.9923
ethnictyWHITE:grade 1.0171 0.5185 1.96 0.0498

2.5.1 anova object


> print(xtable(anova(fm3)), floating = FALSE)

Df Deviance Resid. Df Resid. Dev


NULL 99 129.49
ethnicty 3 47.24 96 82.25
grade 1 1.73 95 80.52
ethnicty:grade 3 7.20 92 73.32

3
2.6 More aov
> N <- c(0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0,
+ 1, 0, 1, 1, 0, 0)
> P <- c(1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0,
+ 0, 1, 0, 1, 1, 0)
> K <- c(1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0,
+ 1, 1, 1, 0, 1, 0)
> yield <- c(49.5, 62.8, 46.8, 57, 59.8, 58.5, 55.5, 56, 62.8,
+ 55.8, 69.5, 55, 62, 48.8, 45.5, 44.2, 52, 51.5, 49.8, 48.8,
+ 57.2, 59, 53.2, 56)
> npk <- data.frame(block = gl(6, 4), N = factor(N), P = factor(P),
+ K = factor(K), yield = yield)
> npk.aov <- aov(yield ~ block + N * P * K, npk)
> op <- options(contrasts = c("contr.helmert", "contr.treatment"))
> npk.aovE <- aov(yield ~ N * P * K + Error(block), npk)
> options(op)

> print(xtable(npk.aov), floating = FALSE)

Df Sum Sq Mean Sq F value Pr(>F)


block 5 343.29 68.66 4.45 0.0159
N 1 189.28 189.28 12.26 0.0044
P 1 8.40 8.40 0.54 0.4749
K 1 95.20 95.20 6.17 0.0288
N:P 1 21.28 21.28 1.38 0.2632
N:K 1 33.13 33.13 2.15 0.1686
P:K 1 0.48 0.48 0.03 0.8628
Residuals 12 185.29 15.44

2.6.1 anova object


> print(xtable(anova(npk.aov)), floating = FALSE)

Df Sum Sq Mean Sq F value Pr(>F)


block 5 343.29 68.66 4.45 0.0159
N 1 189.28 189.28 12.26 0.0044
P 1 8.40 8.40 0.54 0.4749
K 1 95.20 95.20 6.17 0.0288
N:P 1 21.28 21.28 1.38 0.2632
N:K 1 33.13 33.13 2.15 0.1686
P:K 1 0.48 0.48 0.03 0.8628
Residuals 12 185.29 15.44

2.6.2 Another anova object


> print(xtable(summary(npk.aov)), floating = FALSE)

4
Df Sum Sq Mean Sq F value Pr(>F)
block 5 343.29 68.66 4.45 0.0159
N 1 189.28 189.28 12.26 0.0044
P 1 8.40 8.40 0.54 0.4749
K 1 95.20 95.20 6.17 0.0288
N:P 1 21.28 21.28 1.38 0.2632
N:K 1 33.13 33.13 2.15 0.1686
P:K 1 0.48 0.48 0.03 0.8628
Residuals 12 185.29 15.44

> print(xtable(npk.aovE), floating = FALSE)

Df Sum Sq Mean Sq F value Pr(>F)


N:P:K 1 37.00 37.00 0.48 0.5252
Residuals 4 306.29 76.57
N 1 189.28 189.28 12.26 0.0044
P 1 8.40 8.40 0.54 0.4749
K 1 95.20 95.20 6.17 0.0288
N:P 1 21.28 21.28 1.38 0.2632
N:K 1 33.14 33.14 2.15 0.1686
P:K 1 0.48 0.48 0.03 0.8628
Residuals1 12 185.29 15.44

> print(xtable(summary(npk.aovE)), floating = FALSE)

Df Sum Sq Mean Sq F value Pr(>F)


N:P:K 1 37.00 37.00 0.48 0.5252
Residuals 4 306.29 76.57
N 1 189.28 189.28 12.26 0.0044
P 1 8.40 8.40 0.54 0.4749
K 1 95.20 95.20 6.17 0.0288
N:P 1 21.28 21.28 1.38 0.2632
N:K 1 33.14 33.14 2.15 0.1686
P:K 1 0.48 0.48 0.03 0.8628
Residuals1 12 185.29 15.44

2.7 More lm
> ctl <- c(4.17, 5.58, 5.18, 6.11, 4.5, 4.61, 5.17, 4.53, 5.33,
+ 5.14)
> trt <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32,
+ 4.69)
> group <- gl(2, 10, 20, labels = c("Ctl", "Trt"))
> weight <- c(ctl, trt)
> lm.D9 <- lm(weight ~ group)

> print(xtable(lm.D9), floating = FALSE)

5
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.0320 0.2202 22.85 0.0000
groupTrt -0.3710 0.3114 -1.19 0.2490
> print(xtable(anova(lm.D9)), floating = FALSE)
Df Sum Sq Mean Sq F value Pr(>F)
group 1 0.69 0.69 1.42 0.2490
Residuals 18 8.73 0.48

2.8 More glm


> counts <- c(18, 17, 15, 20, 10, 20, 25, 13, 12)
> outcome <- gl(3, 1, 9)
> treatment <- gl(3, 3)
> d.AD <- data.frame(treatment, outcome, counts)
> glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())
> print(xtable(glm.D93, align = "r|llrc"), floating = FALSE)
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.0445 0.1709 17.81 0.0000
outcome2 -0.4543 0.2022 -2.25 0.0246
outcome3 -0.2930 0.1927 -1.52 0.1285
treatment2 0.0000 0.2000 0.00 1.0000
treatment3 0.0000 0.2000 0.00 1.0000

2.9 prcomp
> if (require(stats, quietly = TRUE)) {
+ data(USArrests)
+ pr1 <- prcomp(USArrests)
+ }
> if (require(stats, quietly = TRUE)) {
+ print(xtable(pr1), floating = FALSE)
+ }
PC1 PC2 PC3 PC4
Murder 0.0417 -0.0448 0.0799 -0.9949
Assault 0.9952 -0.0588 -0.0676 0.0389
UrbanPop 0.0463 0.9769 -0.2005 -0.0582
Rape 0.0752 0.2007 0.9741 0.0723
> print(xtable(summary(pr1)), floating = FALSE)
PC1 PC2 PC3 PC4
Standard deviation 83.7324 14.2124 6.4894 2.4828
Proportion of Variance 0.9655 0.0278 0.0058 0.0008
Cumulative Proportion 0.9655 0.9933 0.9991 1.0000

6
2.10 Time series
> temp.ts <- ts(cumsum(1 + round(rnorm(100), 0)), start = c(1954,
+ 7), frequency = 12)
> temp.table <- xtable(temp.ts, digits = 0)
> caption(temp.table) <- "Time series example"

> print(temp.table, floating = FALSE)

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1954 -1 -1 0 3 4 5
1955 5 5 4 4 3 4 4 5 6 7 7 8
1956 8 9 9 12 13 14 14 16 16 17 17 19
1957 21 23 25 28 28 30 32 34 34 35 35 36
1958 37 38 39 41 44 44 43 43 44 45 45 46
1959 48 48 49 51 51 51 52 52 53 52 53 56
1960 59 59 60 61 62 63 63 65 65 66 67 67
1961 69 70 73 75 76 75 78 80 79 80 79 81
1962 82 84 87 89 90 92 93 96 98 100

3 Sanitization
> insane <- data.frame(Name = c("Ampersand", "Greater than", "Less than",
+ "Underscore", "Per cent", "Dollar", "Backslash", "Hash",
+ "Caret", "Tilde", "Left brace", "Right brace"), Character = I(c("&",
+ ">", "<", "_", "%", "$", "\\", "#", "^", "~", "{", "}")))
> colnames(insane)[2] <- paste(insane[, 2], collapse = "")

> print(xtable(insane))

Name &>< %$\#^˜{}


1 Ampersand &
2 Greater than >
3 Less than <
4 Underscore
5 Per cent %
6 Dollar $
7 Backslash \
8 Hash #
9 Caret ^
10 Tilde ˜
11 Left brace {
12 Right brace }

Sometimes you might want to have your own sanitization function

7
> wanttex <- xtable(data.frame(label = paste("Value_is $10^{-",
+ 1:3, "}$", sep = "")))

> print(wanttex, sanitize.text.function = function(str) gsub("_",


+ "\\_", str, fixed = TRUE))

label
1 Value is 10−1
2 Value is 10−2
3 Value is 10−3

3.1 Markup in tables


Markup can be kept in tables, including column and row names, by using a
custom sanitize.text.function:

> mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2), c(1, 5)), 4)
> rownames(mat) <- "$y_{t-1}$"
> colnames(mat) <- c("$R^2$", "$\\bar{R}^2$", "F-stat", "S.E.E",
+ "DW")
> mat <- xtable(mat)

> print(mat, sanitize.text.function = function(x) {


+ x
+ })

R2 R̄2 F-stat S.E.E DW


yt−1 0.90 0.89 200.00 0.04 2.00

You can also have sanitize functions that are specific to column or row names.
In the table below, the row name is not sanitized but column names and table
elements are:
> money <- matrix(c("$1,000", "$900", "$100"), ncol = 3, dimnames = list("$\\alpha$",
+ c("Income (US$)", "Expenses (US$)", "Profit (US$)")))

> print(xtable(money), sanitize.rownames.function = function(x) {


+ x
+ })

8
Income (US$) Expenses (US$) Profit (US$)
α $1,000 $900 $100

4 Format examples
4.1 Adding a centering environment
> print(xtable(lm.D9, caption = "\\tt latex.environment=NULL"),
+ latex.environment = NULL)

Estimate Std. Error t value Pr(>|t|)


(Intercept) 5.0320 0.2202 22.85 0.0000
groupTrt -0.3710 0.3114 -1.19 0.2490

Table 1: latex.environment=NULL

> print(xtable(lm.D9, caption = "\\tt latex.environment=\"\""),


+ latex.environment = "")

Estimate Std. Error t value Pr(>|t|)


(Intercept) 5.0320 0.2202 22.85 0.0000
groupTrt -0.3710 0.3114 -1.19 0.2490

Table 2: latex.environment=""

> print(xtable(lm.D9, caption = "\\tt latex.environment=\"center\""),


+ latex.environment = "center")

4.2 Column alignment


> tli.table <- xtable(tli[1:10, ])

> align(tli.table) <- rep("r", 6)

> print(tli.table, floating = FALSE)

9
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.0320 0.2202 22.85 0.0000
groupTrt -0.3710 0.3114 -1.19 0.2490

Table 3: latex.environment="center"

grade sex disadvg ethnicty tlimth


1 6 M YES HISPANIC 43
2 7 M NO BLACK 88
3 5 F YES HISPANIC 34
4 3 M YES HISPANIC 65
5 8 M YES WHITE 75
6 5 M NO BLACK 74
7 8 F YES HISPANIC 72
8 4 M YES BLACK 79
9 6 M NO WHITE 88
10 7 M YES HISPANIC 87

4.2.1 Single string and column lines


> align(tli.table) <- "|rrl|l|lr|"

> print(tli.table, floating = FALSE)

grade sex disadvg ethnicty tlimth


1 6 M YES HISPANIC 43
2 7 M NO BLACK 88
3 5 F YES HISPANIC 34
4 3 M YES HISPANIC 65
5 8 M YES WHITE 75
6 5 M NO BLACK 74
7 8 F YES HISPANIC 72
8 4 M YES BLACK 79
9 6 M NO WHITE 88
10 7 M YES HISPANIC 87

4.2.2 Fixed width columns


> align(tli.table) <- "|rr|lp{3cm}l|r|"

> print(tli.table, floating = FALSE)

10
grade sex disadvg ethnicty tlimth
1 6 M YES HISPANIC 43
2 7 M NO BLACK 88
3 5 F YES HISPANIC 34
4 3 M YES HISPANIC 65
5 8 M YES WHITE 75
6 5 M NO BLACK 74
7 8 F YES HISPANIC 72
8 4 M YES BLACK 79
9 6 M NO WHITE 88
10 7 M YES HISPANIC 87

4.3 Significant digits


Specify with a single argument
> digits(tli.table) <- 3
> print(tli.table, floating = FALSE, )
grade sex disadvg ethnicty tlimth
1 6 M YES HISPANIC 43
2 7 M NO BLACK 88
3 5 F YES HISPANIC 34
4 3 M YES HISPANIC 65
5 8 M YES WHITE 75
6 5 M NO BLACK 74
7 8 F YES HISPANIC 72
8 4 M YES BLACK 79
9 6 M NO WHITE 88
10 7 M YES HISPANIC 87
or one for each column, counting the row names
> digits(tli.table) <- 1:(ncol(tli) + 1)
> print(tli.table, floating = FALSE, )
grade sex disadvg ethnicty tlimth
1 6 M YES HISPANIC 43
2 7 M NO BLACK 88
3 5 F YES HISPANIC 34
4 3 M YES HISPANIC 65
5 8 M YES WHITE 75
6 5 M NO BLACK 74
7 8 F YES HISPANIC 72
8 4 M YES BLACK 79
9 6 M NO WHITE 88
10 7 M YES HISPANIC 87
or as a full matrix

11
> digits(tli.table) <- matrix(0:4, nrow = 10, ncol = ncol(tli) +
+ 1)
> print(tli.table, floating = FALSE, )
grade sex disadvg ethnicty tlimth
1 6 M YES HISPANIC 43
2 7 M NO BLACK 88
3 5 F YES HISPANIC 34
4 3 M YES HISPANIC 65
5 8 M YES WHITE 75
6 5 M NO BLACK 74
7 8 F YES HISPANIC 72
8 4 M YES BLACK 79
9 6 M NO WHITE 88
10 7 M YES HISPANIC 87

4.4 Suppress row names


> print((tli.table), include.rownames = FALSE, floating = FALSE)
grade sex disadvg ethnicty tlimth
6 M YES HISPANIC 43
7 M NO BLACK 88
5 F YES HISPANIC 34
3 M YES HISPANIC 65
8 M YES WHITE 75
5 M NO BLACK 74
8 F YES HISPANIC 72
4 M YES BLACK 79
6 M NO WHITE 88
7 M YES HISPANIC 87
If you want a vertical line on the left, you need to change the align attribute.
> align(tli.table) <- "|r|r|lp{3cm}l|r|"
> print((tli.table), include.rownames = FALSE, floating = FALSE)
grade sex disadvg ethnicty tlimth
6 M YES HISPANIC 43
7 M NO BLACK 88
5 F YES HISPANIC 34
3 M YES HISPANIC 65
8 M YES WHITE 75
5 M NO BLACK 74
8 F YES HISPANIC 72
4 M YES BLACK 79
6 M NO WHITE 88
7 M YES HISPANIC 87

12
Revert the alignment to what is was before.
> align(tli.table) <- "|rr|lp{3cm}l|r|"

4.5 Suppress column names


> print((tli.table), include.colnames = FALSE, floating = FALSE)

1 6 M YES HISPANIC 43
2 7 M NO BLACK 88
3 5 F YES HISPANIC 34
4 3 M YES HISPANIC 65
5 8 M YES WHITE 75
6 5 M NO BLACK 74
7 8 F YES HISPANIC 72
8 4 M YES BLACK 79
9 6 M NO WHITE 88
10 7 M YES HISPANIC 87
Note the doubled header lines which can be suppressed with, eg,
> print(tli.table, include.colnames = FALSE, floating = FALSE,
+ hline.after = c(0, nrow(tli.table)))
1 6 M YES HISPANIC 43
2 7 M NO BLACK 88
3 5 F YES HISPANIC 34
4 3 M YES HISPANIC 65
5 8 M YES WHITE 75
6 5 M NO BLACK 74
7 8 F YES HISPANIC 72
8 4 M YES BLACK 79
9 6 M NO WHITE 88
10 7 M YES HISPANIC 87

4.6 Suppress row and column names


> print((tli.table), include.colnames = FALSE, include.rownames = FALSE,
+ floating = FALSE)

6 M YES HISPANIC 43
7 M NO BLACK 88
5 F YES HISPANIC 34
3 M YES HISPANIC 65
8 M YES WHITE 75
5 M NO BLACK 74
8 F YES HISPANIC 72
4 M YES BLACK 79
6 M NO WHITE 88
7 M YES HISPANIC 87

13
4.7 Horizontal lines
> print(xtable(anova(glm.D93)), hline.after = c(1), floating = FALSE)

Df Deviance Resid. Df Resid. Dev


NULL 8 10.58
outcome 2 5.45 6 5.13
treatment 2 0.00 4 5.13

4.8 Table-level LATEX


> print(xtable(anova(glm.D93)), size = "small", floating = FALSE)

Df Deviance Resid. Df Resid. Dev


NULL 8 10.58
outcome 2 5.45 6 5.13
treatment 2 0.00 4 5.13

4.9 Long tables


Remember to insert \usepackage{longtable} in your LaTeX preamble. See
Table 4.

> x <- matrix(rnorm(1000), ncol = 10)


> x.big <- xtable(x, label = "tabbig", caption = "Example of longtable spanning several page

> print(x.big, tabular.environment = "longtable", floating = FALSE)

1 2 3 4 5 6 7 8 9 10
1 0.17 0.60 -0.73 2.50 0.87 0.31 0.64 -1.02 -0.94 0.13
2 -0.32 -0.13 -0.13 0.46 -0.13 0.50 -1.03 1.28 0.90 0.18
3 0.50 -0.51 2.27 2.12 1.17 -0.11 -1.11 -0.10 -1.30 1.51
4 1.04 1.08 -0.44 -0.30 0.47 -0.03 1.28 -0.59 -0.11 -0.92
5 -0.77 -1.16 -0.22 0.32 0.92 0.51 1.68 -1.41 0.12 0.92
6 1.21 -0.11 -1.32 0.74 0.85 -0.23 0.12 -0.67 -0.87 -0.20
7 -1.02 -0.42 -1.39 1.12 1.35 0.13 0.90 -0.54 -0.72 -0.85
8 -0.23 -1.74 1.81 0.21 2.88 0.13 -0.25 0.57 -1.24 0.01
9 0.75 -1.94 0.05 -1.18 -0.53 -0.30 0.27 2.20 -0.68 -0.11
10 1.04 -0.85 -0.15 0.12 0.11 -1.18 -0.83 1.02 0.58 -0.24
11 0.57 -0.82 0.44 0.85 0.99 -1.13 1.42 0.14 0.62 0.72
12 0.63 -0.60 0.62 1.07 0.74 -0.41 -0.25 0.14 1.10 -0.27
13 0.08 0.06 0.63 -1.77 -0.38 0.23 -0.22 -0.94 -0.92 -0.41
14 0.37 1.23 -0.16 2.59 0.96 0.39 0.37 1.44 -0.69 -1.55
15 -0.22 0.41 1.10 -1.03 0.82 1.69 -1.80 1.09 -0.60 -0.21
16 0.40 -0.40 -0.23 0.66 -1.25 0.29 -1.42 -0.04 0.26 0.57
17 1.03 -0.25 0.68 -0.64 -1.44 1.72 0.38 1.94 -0.49 -0.14
18 -0.30 -0.04 -0.87 1.26 1.06 -0.57 0.51 0.80 0.56 -0.27

14
19 -1.11 -0.91 1.62 -1.96 -1.26 -0.29 0.63 0.69 0.37 -0.45
20 -1.49 -1.69 -0.39 -0.81 0.44 0.50 0.32 1.07 -0.22 0.75
21 -1.11 -1.25 1.20 0.53 -0.43 -0.64 -2.64 -0.06 0.73 -0.07
22 0.70 -1.28 1.46 -1.52 1.37 0.58 -1.25 0.30 1.49 0.40
23 -1.86 0.40 1.55 -2.04 -0.27 0.95 -1.29 -0.27 -1.59 -0.74
24 -1.06 0.91 -0.77 -2.86 -0.70 -0.26 -0.82 0.67 0.70 -0.95
25 0.30 -0.27 1.10 -0.35 0.31 0.49 -0.78 0.01 1.80 -2.56
26 -1.79 0.38 -0.98 -0.33 -0.89 0.09 0.82 1.02 0.82 0.57
27 -1.39 0.17 -0.96 -1.08 0.76 0.25 0.89 -3.11 -0.68 1.02
28 0.26 -0.40 1.13 0.17 0.53 0.38 0.81 0.83 -1.04 -0.23
29 -0.07 0.52 0.73 0.60 0.21 -0.69 -0.31 0.86 0.55 -0.60
30 -1.04 0.70 -0.65 0.92 0.16 -0.48 1.30 0.30 0.49 -0.12
31 2.18 1.53 0.45 1.38 -0.46 -1.86 -0.72 0.09 0.01 -0.75
32 1.45 -0.53 -0.60 -1.01 0.44 -1.20 1.24 -0.05 -1.63 0.16
33 -0.43 1.12 0.02 0.18 2.95 -0.50 0.32 -0.06 2.13 -0.75
34 -0.50 -0.01 -1.55 -2.03 1.51 0.43 -0.03 0.49 0.18 0.42
35 -0.09 -0.53 -0.14 1.11 0.35 0.05 0.51 0.88 -1.18 -0.14
36 -1.29 1.06 0.95 -0.64 1.12 1.78 -0.47 0.72 0.16 -1.84
37 -1.14 -0.20 0.41 0.25 0.83 -1.24 0.22 -0.76 -0.16 -0.20
38 0.05 -0.58 0.23 0.73 -1.66 -1.28 0.72 -1.32 -0.29 -1.14
39 0.09 -0.44 -0.70 0.06 -0.86 0.16 0.82 -0.41 -0.33 -2.35
40 0.57 -1.90 0.39 -0.30 0.70 -1.02 -0.79 1.51 -1.34 0.96
41 1.02 -0.06 -1.77 -0.96 1.20 -1.55 -0.38 0.19 0.98 -1.07
42 1.71 -0.00 0.18 -0.79 0.13 0.14 -1.84 -1.28 1.78 0.15
43 0.45 -0.68 -1.99 -0.06 1.80 -1.64 -0.98 1.45 -0.68 0.64
44 0.67 -0.27 1.07 -0.42 -0.55 -0.79 0.93 0.24 1.94 0.11
45 -0.93 -0.76 -2.23 0.26 2.00 1.75 0.73 -1.34 -0.43 -0.57
46 0.17 -0.54 -0.63 0.80 0.60 0.52 -0.43 -1.21 0.02 -1.69
47 -0.25 -0.15 0.35 0.68 -0.98 0.44 -0.40 0.52 1.68 -0.27
48 -1.69 -0.74 -0.23 1.39 -0.20 -0.53 0.36 -2.12 0.66 -2.06
49 0.43 -0.57 2.44 -0.21 -0.22 -0.70 -0.14 -0.31 0.02 0.53
50 -1.11 0.52 1.68 -0.34 1.39 -0.19 -0.83 0.96 -0.12 0.00
51 -0.79 -1.76 -0.02 -1.58 0.28 -1.02 -0.34 -0.85 0.44 0.13
52 0.73 -1.52 -0.54 -1.55 -1.00 -0.09 0.91 0.31 0.65 0.78
53 -0.42 0.56 0.05 -0.47 0.38 0.78 -0.93 0.00 -2.44 -0.23
54 0.47 0.69 -0.61 -0.45 -2.13 -0.52 0.35 -0.72 -1.40 -0.62
55 0.04 0.86 -1.36 -0.11 2.23 0.83 0.50 -0.52 2.21 0.48
56 -1.22 -0.91 -1.67 1.41 -2.10 -0.94 -0.40 -1.91 0.45 0.19
57 -1.12 0.36 -1.26 -1.29 -0.03 -1.09 1.75 -0.31 -0.32 0.88
58 -1.36 -0.21 -0.79 0.10 -0.02 -0.78 0.50 -1.14 -1.02 -1.10
59 -0.76 -0.73 -0.84 0.36 1.62 -0.42 -0.27 0.38 -0.05 -1.32
60 -0.41 -0.35 0.32 0.57 1.02 -0.51 -0.91 -0.36 0.82 0.02
61 0.19 0.03 0.34 0.10 1.35 -0.47 -1.23 0.45 0.80 -0.08
62 -1.82 -0.12 -0.83 0.81 0.70 1.16 0.30 -0.02 -0.17 0.05
63 0.36 0.30 1.00 -2.68 -1.52 -0.27 -0.20 1.54 -2.77 -2.14
64 -1.30 0.26 -0.40 -0.60 0.76 -0.06 -0.34 -0.76 1.02 -0.75

15
65 -0.55 -0.94 2.18 0.23 0.87 1.20 0.02 0.87 -0.16 0.47
66 1.63 -0.03 -0.70 0.09 -0.05 -0.10 -1.51 -0.95 1.68 0.57
67 -0.75 -0.51 1.60 -1.19 0.54 -1.07 -0.25 0.42 1.25 -0.94
68 -1.45 -0.11 -0.45 0.34 -1.04 -0.72 0.58 0.66 -0.11 -0.39
69 0.05 0.94 -0.31 -0.03 -1.26 1.16 0.22 0.48 -2.54 -2.45
70 0.62 -0.39 0.37 -0.68 -0.98 -1.35 -0.99 0.04 0.15 0.04
71 -0.04 -1.82 0.46 0.22 -1.43 0.01 -0.14 0.21 0.88 1.09
72 0.51 0.30 0.79 0.82 0.58 0.56 0.71 -0.38 -0.23 -0.44
73 0.17 -0.35 0.57 0.77 -1.25 1.87 0.33 -0.27 -1.59 -0.12
74 -0.63 0.34 1.03 -1.23 -0.51 -0.07 -0.09 0.58 -1.31 -0.39
75 1.64 1.17 1.45 -2.43 1.74 -1.38 0.05 -0.62 -0.02 0.22
76 -1.23 -0.80 0.76 1.08 0.95 3.03 -1.15 -0.54 -2.73 -0.22
77 1.07 0.68 0.47 -3.11 0.23 -1.36 0.31 0.37 1.05 -0.12
78 2.64 -0.73 0.30 1.34 -0.35 -0.03 0.79 1.55 1.83 -0.38
79 -0.69 -0.85 0.68 0.03 1.60 -0.36 0.27 0.97 0.09 0.50
80 0.98 0.23 -1.15 0.23 0.26 -0.08 -0.61 0.47 0.84 -0.15
81 -0.97 0.39 -1.37 1.04 -0.07 -0.07 0.24 1.37 -0.28 -0.93
82 0.30 -0.13 -0.28 -1.26 -0.57 -1.78 0.02 0.36 -0.81 0.82
83 0.29 -0.62 1.18 0.92 1.65 -1.05 0.04 0.54 0.24 -0.08
84 -1.99 0.18 -1.61 1.36 0.23 -1.13 -1.26 0.23 1.60 0.81
85 1.26 -1.58 0.92 -0.46 0.41 -0.84 -1.40 -0.56 0.92 1.35
86 1.39 2.29 -0.50 0.17 -1.80 -0.07 -0.27 -1.42 -0.76 1.20
87 -0.47 0.94 -0.14 -1.33 -1.47 -1.06 -0.35 -0.12 0.01 1.92
88 -1.35 -2.10 -0.18 -0.74 0.95 -0.64 -0.62 -0.52 -0.87 0.17
89 -0.27 0.37 -0.13 1.12 1.17 0.38 -0.40 1.13 0.69 -0.04
90 1.35 -0.10 -1.89 -0.71 -0.62 0.39 0.33 0.04 -2.14 0.47
91 0.82 -0.74 -0.38 -0.07 -0.60 0.67 -0.96 -0.33 -0.57 0.43
92 0.25 0.57 0.36 -0.38 -1.62 -1.60 2.79 1.06 -0.98 0.87
93 0.70 -0.37 0.81 -0.98 0.54 0.78 1.36 0.20 1.03 -2.13
94 0.04 -0.60 -0.14 0.68 -0.33 0.65 0.47 1.02 0.43 0.58
95 0.19 -1.10 -0.16 0.55 0.07 1.82 -1.35 -0.04 0.18 -1.52
96 0.40 -1.14 0.12 1.12 0.19 -0.30 -0.72 -0.72 0.44 1.23
97 -0.24 0.89 -0.45 -0.88 0.47 -0.73 0.14 -1.58 1.31 -1.14
98 1.39 1.65 -1.91 0.99 1.35 1.35 0.21 2.29 1.00 -1.12
99 1.05 -2.01 0.81 -1.18 -0.12 1.41 -0.35 -1.70 0.74 0.79
100 1.98 -0.53 1.06 -1.36 -0.20 -0.23 -0.75 -0.24 -0.74 -0.92
Table 4: Example of longtable spanning several pages

4.10 Sideways tables


Remember to insert \usepackage{rotating} in your LaTeX preamble. Side-
ways tables can’t be forced in place with the ‘H’ specifier, but you can use the
\clearpage command to get them fairly nearby. See Table 5.

16
> x <- x[1:30, ]
> x.small <- xtable(x, label = "tabsmall", caption = "A sideways table")

> print(x.small, floating.environment = "sidewaystable")

17
1 2 3 4 5 6 7 8 9 10
1 0.17 0.60 -0.73 2.50 0.87 0.31 0.64 -1.02 -0.94 0.13
2 -0.32 -0.13 -0.13 0.46 -0.13 0.50 -1.03 1.28 0.90 0.18
3 0.50 -0.51 2.27 2.12 1.17 -0.11 -1.11 -0.10 -1.30 1.51
4 1.04 1.08 -0.44 -0.30 0.47 -0.03 1.28 -0.59 -0.11 -0.92
5 -0.77 -1.16 -0.22 0.32 0.92 0.51 1.68 -1.41 0.12 0.92
6 1.21 -0.11 -1.32 0.74 0.85 -0.23 0.12 -0.67 -0.87 -0.20
7 -1.02 -0.42 -1.39 1.12 1.35 0.13 0.90 -0.54 -0.72 -0.85
8 -0.23 -1.74 1.81 0.21 2.88 0.13 -0.25 0.57 -1.24 0.01
9 0.75 -1.94 0.05 -1.18 -0.53 -0.30 0.27 2.20 -0.68 -0.11
10 1.04 -0.85 -0.15 0.12 0.11 -1.18 -0.83 1.02 0.58 -0.24
11 0.57 -0.82 0.44 0.85 0.99 -1.13 1.42 0.14 0.62 0.72
12 0.63 -0.60 0.62 1.07 0.74 -0.41 -0.25 0.14 1.10 -0.27
13 0.08 0.06 0.63 -1.77 -0.38 0.23 -0.22 -0.94 -0.92 -0.41
14 0.37 1.23 -0.16 2.59 0.96 0.39 0.37 1.44 -0.69 -1.55
15 -0.22 0.41 1.10 -1.03 0.82 1.69 -1.80 1.09 -0.60 -0.21
16 0.40 -0.40 -0.23 0.66 -1.25 0.29 -1.42 -0.04 0.26 0.57

18
17 1.03 -0.25 0.68 -0.64 -1.44 1.72 0.38 1.94 -0.49 -0.14
18 -0.30 -0.04 -0.87 1.26 1.06 -0.57 0.51 0.80 0.56 -0.27
19 -1.11 -0.91 1.62 -1.96 -1.26 -0.29 0.63 0.69 0.37 -0.45
20 -1.49 -1.69 -0.39 -0.81 0.44 0.50 0.32 1.07 -0.22 0.75
21 -1.11 -1.25 1.20 0.53 -0.43 -0.64 -2.64 -0.06 0.73 -0.07
22 0.70 -1.28 1.46 -1.52 1.37 0.58 -1.25 0.30 1.49 0.40
23 -1.86 0.40 1.55 -2.04 -0.27 0.95 -1.29 -0.27 -1.59 -0.74
24 -1.06 0.91 -0.77 -2.86 -0.70 -0.26 -0.82 0.67 0.70 -0.95
25 0.30 -0.27 1.10 -0.35 0.31 0.49 -0.78 0.01 1.80 -2.56
26 -1.79 0.38 -0.98 -0.33 -0.89 0.09 0.82 1.02 0.82 0.57
27 -1.39 0.17 -0.96 -1.08 0.76 0.25 0.89 -3.11 -0.68 1.02
28 0.26 -0.40 1.13 0.17 0.53 0.38 0.81 0.83 -1.04 -0.23
29 -0.07 0.52 0.73 0.60 0.21 -0.69 -0.31 0.86 0.55 -0.60
30 -1.04 0.70 -0.65 0.92 0.16 -0.48 1.30 0.30 0.49 -0.12

Table 5: A sideways table


5 Acknowledgements
Most of the examples in this gallery are taken from the xtable documentation.

6 R Session information
> toLatex(sessionInfo())

• R version 2.9.1 (2009-06-26), x86_64-unknown-linux-gnu


• Locale: LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-
8;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_
8;LC_IDENTIFICATION=C

• Base packages: base, datasets, graphics, grDevices, methods, stats, tools,


utils
• Other packages: xtable 1.5-6

19

You might also like