Skip to content

Tables


Foreword

  • Output options: ‘pygments’ syntax, the ‘readable’ theme.
  • Snippets and results.

Markdown tables

Example 1

1
2
3
4
5
6
7
| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
|   12  |  12  |    12   |    12  |
|  123  |  123 |   123   |   123  |
|    1  |    1 |     1   |     1  |

: Demonstration of pipe table syntax.
Demonstration of pipe table syntax.
Right Left Default Center
12 12 12 12
123 123 123 123
1 1 1 1

Example 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
: Sample grid table.

+---------------+---------------+--------------------+
| Fruit         | Price         | Advantages         |
+===============+===============+====================+
| Bananas       | $1.34         | - built-in wrapper |
|               |               | - bright color     |
+---------------+---------------+--------------------+
| Oranges       | $2.10         | - cures scurvy     |
|               |               | - tasty            |
+---------------+---------------+--------------------+
Sample grid table.
Fruit Price Advantages

Bananas

$1.34

  • built-in wrapper
  • bright color

Oranges

$2.10

  • cures scurvy
  • tasty

The xtable package

The output is in HTML.

Example 1

library(xtable)

# given the data in the first row
print(xtable(output,
             caption = 'A test table', 
             align = c('l', 'c', 'r')),
      type = 'html')
A test table
1st header 2nd header
1st row Content A Content B
2nd row Content C Content D

The knitr::kable function

The output is in Markdown.

Example 1

library(knitr)

# given the data in the first row
kable(output, 
      caption = 'A test table', 
      align = c('c', 'r'))
A test table
1st header 2nd header
1st row Content A Content B
2nd row Content C Content D

We can also write knitr::kable() without calling library(knitr).

The htmlTable package

htmlTable on GitHub.

Example 1

output <- 
  matrix(paste('Content', LETTERS[1:16]), 
         ncol = 4, byrow = TRUE)

library(htmlTable)

htmlTable(output,
          header = paste(c('1st', '2nd', '3rd', '4th'), 'header'),
          rnames = paste(c('1st', '2nd', '3rd', '4th'), 'row'),
          rgroup = c('Group A', 'Group B'),
          n.rgroup = c(2,2),
          cgroup = c('Cgroup 1', 'Cgroup 2&dagger;'),
          n.cgroup = c(2,2), 
          caption = 'Basic table with both column spanners (groups) and row groups',
          tfoot = '&dagger; A table footer commment')
Basic table with both column spanners (groups) and row groups
Cgroup 1   Cgroup 2†
1st header 2nd header   3rd header 4th header
Group A
  1st row Content A Content B   Content C Content D
  2nd row Content E Content F   Content G Content H
Group B
  3rd row Content I Content J   Content K Content L
  4th row Content M Content N   Content O Content P
† A table footer commment

Example 2

library(htmlTable)

# given the data in the first row
htmlTable(txtRound(mx, 1), 
          cgroup = cgroup,
          n.cgroup = n.cgroup,
          rgroup = c('First period', 'Second period', 'Third period'),
          n.rgroup = rep(5, 3),
          tfoot = txtMergeLines('&Delta;<sub>int</sub> correspnds to the change since start',
                                '&Delta;<sub>std</sub> corresponds to the change compared to national average'))
Sweden   Norrbotten county   Stockholm county   Uppsala county
Men   Women   Men   Women   Men   Women   Men   Women
Age Δint   Age Δint   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd
First period
  1999 38.9 0.0   41.5 0.0   39.7 0.0 0.8   41.9 0.0 0.4   37.3 0.0 -1.6   40.1 0.0 -1.4   37.2 0.0 -1.7   39.3 0.0 -2.2
  2000 39.0 0.1   41.6 0.1   40.0 0.3 1.0   42.2 0.3 0.6   37.4 0.1 -1.6   40.1 0.0 -1.5   37.5 0.3 -1.5   39.4 0.1 -2.2
  2001 39.2 0.3   41.7 0.2   40.2 0.5 1.0   42.5 0.6 0.8   37.5 0.2 -1.7   40.1 0.0 -1.6   37.6 0.4 -1.6   39.6 0.3 -2.1
  2002 39.3 0.4   41.8 0.3   40.5 0.8 1.2   42.8 0.9 1.0   37.6 0.3 -1.7   40.2 0.1 -1.6   37.8 0.6 -1.5   39.7 0.4 -2.1
  2003 39.4 0.5   41.9 0.4   40.7 1.0 1.3   43.0 1.1 1.1   37.7 0.4 -1.7   40.2 0.1 -1.7   38.0 0.8 -1.4   39.8 0.5 -2.1
Second period
  2004 39.6 0.7   42.0 0.5   40.9 1.2 1.3   43.1 1.2 1.1   37.8 0.5 -1.8   40.3 0.2 -1.7   38.1 0.9 -1.5   40.0 0.7 -2.0
  2005 39.7 0.8   42.0 0.5   41.1 1.4 1.4   43.4 1.5 1.4   37.9 0.6 -1.8   40.3 0.2 -1.7   38.3 1.1 -1.4   40.1 0.8 -1.9
  2006 39.8 0.9   42.1 0.6   41.3 1.6 1.5   43.5 1.6 1.4   37.9 0.6 -1.9   40.2 0.1 -1.9   38.5 1.3 -1.3   40.4 1.1 -1.7
  2007 39.8 0.9   42.1 0.6   41.5 1.8 1.7   43.8 1.9 1.7   37.8 0.5 -2.0   40.1 0.0 -2.0   38.6 1.4 -1.2   40.5 1.2 -1.6
  2008 39.9 1.0   42.1 0.6   41.7 2.0 1.8   44.0 2.1 1.9   37.8 0.5 -2.1   40.1 0.0 -2.0   38.7 1.5 -1.2   40.5 1.2 -1.6
Third period
  2009 39.9 1.0   42.1 0.6   41.9 2.2 2.0   44.2 2.3 2.1   37.8 0.5 -2.1   40.0 -0.1 -2.1   38.8 1.6 -1.1   40.6 1.3 -1.5
  2010 40.0 1.1   42.1 0.6   42.1 2.4 2.1   44.4 2.5 2.3   37.8 0.5 -2.2   40.0 -0.1 -2.1   38.9 1.7 -1.1   40.6 1.3 -1.5
  2011 40.1 1.2   42.2 0.7   42.3 2.6 2.2   44.5 2.6 2.3   37.9 0.6 -2.2   39.9 -0.2 -2.3   39.0 1.8 -1.1   40.7 1.4 -1.5
  2012 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.6 2.7 2.4   37.9 0.6 -2.3   39.9 -0.2 -2.3   39.1 1.9 -1.1   40.8 1.5 -1.4
  2013 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.7 2.8 2.5   38.0 0.7 -2.2   39.9 -0.2 -2.3   39.2 2.0 -1.0   40.9 1.6 -1.3
Δint correspnds to the change since start

Δstd corresponds to the change compared to national average

Example 3

library(htmlTable)

# given the data in the first row
htmlTable(txtRound(mx, 1), 
          align = 'rrrr|r',
          cgroup = cgroup,
          n.cgroup = n.cgroup,
          rgroup = c('First period', 'Second period', 'Third period'),
          n.rgroup = rep(5, 3),
          tfoot = txtMergeLines('&Delta;<sub>int</sub> correspnds to the change since start',
                                '&Delta;<sub>std</sub> corresponds to the change compared to national average'))
Sweden   Norrbotten county   Stockholm county   Uppsala county
Men   Women   Men   Women   Men   Women   Men   Women
Age Δint   Age Δint   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd
First period
  1999 38.9 0.0   41.5 0.0   39.7 0.0 0.8   41.9 0.0 0.4   37.3 0.0 -1.6   40.1 0.0 -1.4   37.2 0.0 -1.7   39.3 0.0 -2.2
  2000 39.0 0.1   41.6 0.1   40.0 0.3 1.0   42.2 0.3 0.6   37.4 0.1 -1.6   40.1 0.0 -1.5   37.5 0.3 -1.5   39.4 0.1 -2.2
  2001 39.2 0.3   41.7 0.2   40.2 0.5 1.0   42.5 0.6 0.8   37.5 0.2 -1.7   40.1 0.0 -1.6   37.6 0.4 -1.6   39.6 0.3 -2.1
  2002 39.3 0.4   41.8 0.3   40.5 0.8 1.2   42.8 0.9 1.0   37.6 0.3 -1.7   40.2 0.1 -1.6   37.8 0.6 -1.5   39.7 0.4 -2.1
  2003 39.4 0.5   41.9 0.4   40.7 1.0 1.3   43.0 1.1 1.1   37.7 0.4 -1.7   40.2 0.1 -1.7   38.0 0.8 -1.4   39.8 0.5 -2.1
Second period
  2004 39.6 0.7   42.0 0.5   40.9 1.2 1.3   43.1 1.2 1.1   37.8 0.5 -1.8   40.3 0.2 -1.7   38.1 0.9 -1.5   40.0 0.7 -2.0
  2005 39.7 0.8   42.0 0.5   41.1 1.4 1.4   43.4 1.5 1.4   37.9 0.6 -1.8   40.3 0.2 -1.7   38.3 1.1 -1.4   40.1 0.8 -1.9
  2006 39.8 0.9   42.1 0.6   41.3 1.6 1.5   43.5 1.6 1.4   37.9 0.6 -1.9   40.2 0.1 -1.9   38.5 1.3 -1.3   40.4 1.1 -1.7
  2007 39.8 0.9   42.1 0.6   41.5 1.8 1.7   43.8 1.9 1.7   37.8 0.5 -2.0   40.1 0.0 -2.0   38.6 1.4 -1.2   40.5 1.2 -1.6
  2008 39.9 1.0   42.1 0.6   41.7 2.0 1.8   44.0 2.1 1.9   37.8 0.5 -2.1   40.1 0.0 -2.0   38.7 1.5 -1.2   40.5 1.2 -1.6
Third period
  2009 39.9 1.0   42.1 0.6   41.9 2.2 2.0   44.2 2.3 2.1   37.8 0.5 -2.1   40.0 -0.1 -2.1   38.8 1.6 -1.1   40.6 1.3 -1.5
  2010 40.0 1.1   42.1 0.6   42.1 2.4 2.1   44.4 2.5 2.3   37.8 0.5 -2.2   40.0 -0.1 -2.1   38.9 1.7 -1.1   40.6 1.3 -1.5
  2011 40.1 1.2   42.2 0.7   42.3 2.6 2.2   44.5 2.6 2.3   37.9 0.6 -2.2   39.9 -0.2 -2.3   39.0 1.8 -1.1   40.7 1.4 -1.5
  2012 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.6 2.7 2.4   37.9 0.6 -2.3   39.9 -0.2 -2.3   39.1 1.9 -1.1   40.8 1.5 -1.4
  2013 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.7 2.8 2.5   38.0 0.7 -2.2   39.9 -0.2 -2.3   39.2 2.0 -1.0   40.9 1.6 -1.3
Δint correspnds to the change since start

Δstd corresponds to the change compared to national average

Example 4

library(htmlTable)

# given the data in the first row
htmlTable(txtRound(mx, 1), 
          col.columns = c(rep('#E6E6F0', 4),
                          rep('none', ncol(mx) - 4)),
          align = 'rrrr|r',
          cgroup = cgroup,
          n.cgroup = n.cgroup,
          rgroup = c('First period', 'Second period', 'Third period'),
          n.rgroup = rep(5, 3),
                    tfoot = txtMergeLines('&Delta;<sub>int</sub> correspnds to the change since start',
                                          '&Delta;<sub>std</sub> corresponds to the change compared to national average'))
Sweden   Norrbotten county   Stockholm county   Uppsala county
Men   Women   Men   Women   Men   Women   Men   Women
Age Δint   Age Δint   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd
First period
  1999 38.9 0.0   41.5 0.0   39.7 0.0 0.8   41.9 0.0 0.4   37.3 0.0 -1.6   40.1 0.0 -1.4   37.2 0.0 -1.7   39.3 0.0 -2.2
  2000 39.0 0.1   41.6 0.1   40.0 0.3 1.0   42.2 0.3 0.6   37.4 0.1 -1.6   40.1 0.0 -1.5   37.5 0.3 -1.5   39.4 0.1 -2.2
  2001 39.2 0.3   41.7 0.2   40.2 0.5 1.0   42.5 0.6 0.8   37.5 0.2 -1.7   40.1 0.0 -1.6   37.6 0.4 -1.6   39.6 0.3 -2.1
  2002 39.3 0.4   41.8 0.3   40.5 0.8 1.2   42.8 0.9 1.0   37.6 0.3 -1.7   40.2 0.1 -1.6   37.8 0.6 -1.5   39.7 0.4 -2.1
  2003 39.4 0.5   41.9 0.4   40.7 1.0 1.3   43.0 1.1 1.1   37.7 0.4 -1.7   40.2 0.1 -1.7   38.0 0.8 -1.4   39.8 0.5 -2.1
Second period
  2004 39.6 0.7   42.0 0.5   40.9 1.2 1.3   43.1 1.2 1.1   37.8 0.5 -1.8   40.3 0.2 -1.7   38.1 0.9 -1.5   40.0 0.7 -2.0
  2005 39.7 0.8   42.0 0.5   41.1 1.4 1.4   43.4 1.5 1.4   37.9 0.6 -1.8   40.3 0.2 -1.7   38.3 1.1 -1.4   40.1 0.8 -1.9
  2006 39.8 0.9   42.1 0.6   41.3 1.6 1.5   43.5 1.6 1.4   37.9 0.6 -1.9   40.2 0.1 -1.9   38.5 1.3 -1.3   40.4 1.1 -1.7
  2007 39.8 0.9   42.1 0.6   41.5 1.8 1.7   43.8 1.9 1.7   37.8 0.5 -2.0   40.1 0.0 -2.0   38.6 1.4 -1.2   40.5 1.2 -1.6
  2008 39.9 1.0   42.1 0.6   41.7 2.0 1.8   44.0 2.1 1.9   37.8 0.5 -2.1   40.1 0.0 -2.0   38.7 1.5 -1.2   40.5 1.2 -1.6
Third period
  2009 39.9 1.0   42.1 0.6   41.9 2.2 2.0   44.2 2.3 2.1   37.8 0.5 -2.1   40.0 -0.1 -2.1   38.8 1.6 -1.1   40.6 1.3 -1.5
  2010 40.0 1.1   42.1 0.6   42.1 2.4 2.1   44.4 2.5 2.3   37.8 0.5 -2.2   40.0 -0.1 -2.1   38.9 1.7 -1.1   40.6 1.3 -1.5
  2011 40.1 1.2   42.2 0.7   42.3 2.6 2.2   44.5 2.6 2.3   37.9 0.6 -2.2   39.9 -0.2 -2.3   39.0 1.8 -1.1   40.7 1.4 -1.5
  2012 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.6 2.7 2.4   37.9 0.6 -2.3   39.9 -0.2 -2.3   39.1 1.9 -1.1   40.8 1.5 -1.4
  2013 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.7 2.8 2.5   38.0 0.7 -2.2   39.9 -0.2 -2.3   39.2 2.0 -1.0   40.9 1.6 -1.3
Δint correspnds to the change since start

Δstd corresponds to the change compared to national average

Example 5

library(htmlTable)

# given the data in the first row
htmlTable(txtRound(mx, 1),
          col.rgroup = c('none', '#FFFFCC'),
          col.columns = c(rep('#EFEFF0', 4),
                          rep('none', ncol(mx) - 4)),
          align = 'rrrr|r',
          cgroup = cgroup,
          n.cgroup = n.cgroup,
          # I use the &nbsp; - the no breaking space as I don't want to have a
          # row break in the row group. This adds a little space in the table
          # when used together with the cspan.rgroup=1.
          rgroup = c('1st&nbsp;period', '2nd&nbsp;period', '3rd&nbsp;period'),
          n.rgroup = rep(5, 3),
          tfoot = txtMergeLines('&Delta;<sub>int</sub> correspnds to the change since start',
                                '&Delta;<sub>std</sub> corresponds to the change compared to national average'),
          cspan.rgroup = 1)
Sweden   Norrbotten county   Stockholm county   Uppsala county
Men   Women   Men   Women   Men   Women   Men   Women
Age Δint   Age Δint   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd
1st period              
  1999 38.9 0.0   41.5 0.0   39.7 0.0 0.8   41.9 0.0 0.4   37.3 0.0 -1.6   40.1 0.0 -1.4   37.2 0.0 -1.7   39.3 0.0 -2.2
  2000 39.0 0.1   41.6 0.1   40.0 0.3 1.0   42.2 0.3 0.6   37.4 0.1 -1.6   40.1 0.0 -1.5   37.5 0.3 -1.5   39.4 0.1 -2.2
  2001 39.2 0.3   41.7 0.2   40.2 0.5 1.0   42.5 0.6 0.8   37.5 0.2 -1.7   40.1 0.0 -1.6   37.6 0.4 -1.6   39.6 0.3 -2.1
  2002 39.3 0.4   41.8 0.3   40.5 0.8 1.2   42.8 0.9 1.0   37.6 0.3 -1.7   40.2 0.1 -1.6   37.8 0.6 -1.5   39.7 0.4 -2.1
  2003 39.4 0.5   41.9 0.4   40.7 1.0 1.3   43.0 1.1 1.1   37.7 0.4 -1.7   40.2 0.1 -1.7   38.0 0.8 -1.4   39.8 0.5 -2.1
2nd period              
  2004 39.6 0.7   42.0 0.5   40.9 1.2 1.3   43.1 1.2 1.1   37.8 0.5 -1.8   40.3 0.2 -1.7   38.1 0.9 -1.5   40.0 0.7 -2.0
  2005 39.7 0.8   42.0 0.5   41.1 1.4 1.4   43.4 1.5 1.4   37.9 0.6 -1.8   40.3 0.2 -1.7   38.3 1.1 -1.4   40.1 0.8 -1.9
  2006 39.8 0.9   42.1 0.6   41.3 1.6 1.5   43.5 1.6 1.4   37.9 0.6 -1.9   40.2 0.1 -1.9   38.5 1.3 -1.3   40.4 1.1 -1.7
  2007 39.8 0.9   42.1 0.6   41.5 1.8 1.7   43.8 1.9 1.7   37.8 0.5 -2.0   40.1 0.0 -2.0   38.6 1.4 -1.2   40.5 1.2 -1.6
  2008 39.9 1.0   42.1 0.6   41.7 2.0 1.8   44.0 2.1 1.9   37.8 0.5 -2.1   40.1 0.0 -2.0   38.7 1.5 -1.2   40.5 1.2 -1.6
3rd period              
  2009 39.9 1.0   42.1 0.6   41.9 2.2 2.0   44.2 2.3 2.1   37.8 0.5 -2.1   40.0 -0.1 -2.1   38.8 1.6 -1.1   40.6 1.3 -1.5
  2010 40.0 1.1   42.1 0.6   42.1 2.4 2.1   44.4 2.5 2.3   37.8 0.5 -2.2   40.0 -0.1 -2.1   38.9 1.7 -1.1   40.6 1.3 -1.5
  2011 40.1 1.2   42.2 0.7   42.3 2.6 2.2   44.5 2.6 2.3   37.9 0.6 -2.2   39.9 -0.2 -2.3   39.0 1.8 -1.1   40.7 1.4 -1.5
  2012 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.6 2.7 2.4   37.9 0.6 -2.3   39.9 -0.2 -2.3   39.1 1.9 -1.1   40.8 1.5 -1.4
  2013 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.7 2.8 2.5   38.0 0.7 -2.2   39.9 -0.2 -2.3   39.2 2.0 -1.0   40.9 1.6 -1.3
Δint correspnds to the change since start

Δstd corresponds to the change compared to national average

Example 6

library(htmlTable)

# given the data in the first row
htmlTable(out_mx,
          caption = 'Average age in Sweden counties over a period of
                     15 years. The Norbotten county is typically known
                     for having a negative migration pattern compared to
                     Stockholm, while Uppsala has a proportionally large 
                     population of students.',
          pos.rowlabel = 'bottom',
          rowlabel='Year', 
          col.rgroup = c('none', '#FFFFCC'),
          col.columns = c(rep('#EFEFF0', 4),
                          rep('none', ncol(mx) - 4)),
          align = 'rrrr|r',
          cgroup = cgroup,
          n.cgroup = n.cgroup,
          rgroup = c('1st&nbsp;period', '2nd&nbsp;period', '3rd&nbsp;period'),
          n.rgroup = rep(5, 3),
          tfoot = txtMergeLines('&Delta;<sub>int</sub> correspnds to the change since start',
                                '&Delta;<sub>std</sub> corresponds to the change compared to national average'),
          cspan.rgroup = 1)
Average age in Sweden counties over a period of 15 years. The Norbotten county is typically known for having a negative migration pattern compared to Stockholm, while Uppsala has a proportionally large population of students.
Sweden   Norrbotten county   Stockholm county   Uppsala county
Men   Women   Men   Women   Men   Women   Men   Women
Year Age Δint   Age Δint   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd
1st period              
  1999 38.9 0.0   41.5 0.0   39.7 0.0 0.8   41.9 0.0 0.4   37.3 0.0 -1.6   40.1 0.0 -1.4   37.2 0.0 -1.7   39.3 0.0 -2.2
  2000 39.0 0.1   41.6 0.1   40.0 0.3 1.0   42.2 0.3 0.6   37.4 0.1 -1.6   40.1 0.0 -1.5   37.5 0.3 -1.5   39.4 0.1 -2.2
  2001 39.2 0.3   41.7 0.2   40.2 0.5 1.0   42.5 0.6 0.8   37.5 0.2 -1.7   40.1 0.0 -1.6   37.6 0.4 -1.6   39.6 0.3 -2.1
  2002 39.3 0.4   41.8 0.3   40.5 0.8 1.2   42.8 0.9 1.0   37.6 0.3 -1.7   40.2 0.1 -1.6   37.8 0.6 -1.5   39.7 0.4 -2.1
  2003 39.4 0.5   41.9 0.4   40.7 1.0 1.3   43.0 1.1 1.1   37.7 0.4 -1.7   40.2 0.1 -1.7   38.0 0.8 -1.4   39.8 0.5 -2.1
2nd period              
  2004 39.6 0.7   42.0 0.5   40.9 1.2 1.3   43.1 1.2 1.1   37.8 0.5 -1.8   40.3 0.2 -1.7   38.1 0.9 -1.5   40.0 0.7 -2.0
  2005 39.7 0.8   42.0 0.5   41.1 1.4 1.4   43.4 1.5 1.4   37.9 0.6 -1.8   40.3 0.2 -1.7   38.3 1.1 -1.4   40.1 0.8 -1.9
  2006 39.8 0.9   42.1 0.6   41.3 1.6 1.5   43.5 1.6 1.4   37.9 0.6 -1.9   40.2 0.1 -1.9   38.5 1.3 -1.3   40.4 1.1 -1.7
  2007 39.8 0.9   42.1 0.6   41.5 1.8 1.7   43.8 1.9 1.7   37.8 0.5 -2.0   40.1 0.0 -2.0   38.6 1.4 -1.2   40.5 1.2 -1.6
  2008 39.9 1.0   42.1 0.6   41.7 2.0 1.8   44.0 2.1 1.9   37.8 0.5 -2.1   40.1 0.0 -2.0   38.7 1.5 -1.2   40.5 1.2 -1.6
3rd period              
  2009 39.9 1.0   42.1 0.6   41.9 2.2 2.0   44.2 2.3 2.1   37.8 0.5 -2.1   40.0 -0.1 -2.1   38.8 1.6 -1.1   40.6 1.3 -1.5
  2010 40.0 1.1   42.1 0.6   42.1 2.4 2.1   44.4 2.5 2.3   37.8 0.5 -2.2   40.0 -0.1 -2.1   38.9 1.7 -1.1   40.6 1.3 -1.5
  2011 40.1 1.2   42.2 0.7   42.3 2.6 2.2   44.5 2.6 2.3   37.9 0.6 -2.2   39.9 -0.2 -2.3   39.0 1.8 -1.1   40.7 1.4 -1.5
  2012 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.6 2.7 2.4   37.9 0.6 -2.3   39.9 -0.2 -2.3   39.1 1.9 -1.1   40.8 1.5 -1.4
  2013 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.7 2.8 2.5   38.0 0.7 -2.2   39.9 -0.2 -2.3   39.2 2.0 -1.0   40.9 1.6 -1.3
Δint correspnds to the change since start

Δstd corresponds to the change compared to national average

Example 7

library(htmlTable)

# given the data in the first row
htmlTable(out_mx,
          caption = 'Average age in Sweden counties over a period of
                     15 years. The Norbotten county is typically known
                     for having a negative migration pattern compared to
                     Stockholm, while Uppsala has a proportionally large 
                     population of students.',
          pos.rowlabel = 'bottom',
          rowlabel = 'Year', 
          col.rgroup = c('none', '#FFFFCC'),
          col.columns = c(rep('#EFEFF0', 4), rep('none', ncol(mx) - 4)),
          align = 'rrrr|r',
          cgroup = cgroup,
          n.cgroup = n.cgroup,
          rgroup = c('1st&nbsp;period', '2nd&nbsp;period', '3rd&nbsp;period'),
          n.rgroup = rep(5, 3),
          tfoot = txtMergeLines('&Delta;<sub>int</sub> correspnds to the change since start',
                                '&Delta;<sub>std</sub> corresponds to the change compared to national average'),
          cspan.rgroup = 1)
Average age in Sweden counties over a period of 15 years. The Norbotten county is typically known for having a negative migration pattern compared to Stockholm, while Uppsala has a proportionally large population of students.
Sweden   Norrbotten county   Stockholm county   Uppsala county
Men   Women   Men   Women   Men   Women   Men   Women
Year Age Δint   Age Δint   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd   Age Δint Δstd
1st period              
  1999 38.9 0.0   41.5 0.0   39.7 0.0 0.8   41.9 0.0 0.4   37.3 0.0 -1.6   40.1 0.0 -1.4   37.2 0.0 -1.7   39.3 0.0 -2.2
  2000 39.0 0.1   41.6 0.1   40.0 0.3 1.0   42.2 0.3 0.6   37.4 0.1 -1.6   40.1 0.0 -1.5   37.5 0.3 -1.5   39.4 0.1 -2.2
  2001 39.2 0.3   41.7 0.2   40.2 0.5 1.0   42.5 0.6 0.8   37.5 0.2 -1.7   40.1 0.0 -1.6   37.6 0.4 -1.6   39.6 0.3 -2.1
  2002 39.3 0.4   41.8 0.3   40.5 0.8 1.2   42.8 0.9 1.0   37.6 0.3 -1.7   40.2 0.1 -1.6   37.8 0.6 -1.5   39.7 0.4 -2.1
  2003 39.4 0.5   41.9 0.4   40.7 1.0 1.3   43.0 1.1 1.1   37.7 0.4 -1.7   40.2 0.1 -1.7   38.0 0.8 -1.4   39.8 0.5 -2.1
2nd period              
  2004 39.6 0.7   42.0 0.5   40.9 1.2 1.3   43.1 1.2 1.1   37.8 0.5 -1.8   40.3 0.2 -1.7   38.1 0.9 -1.5   40.0 0.7 -2.0
  2005 39.7 0.8   42.0 0.5   41.1 1.4 1.4   43.4 1.5 1.4   37.9 0.6 -1.8   40.3 0.2 -1.7   38.3 1.1 -1.4   40.1 0.8 -1.9
  2006 39.8 0.9   42.1 0.6   41.3 1.6 1.5   43.5 1.6 1.4   37.9 0.6 -1.9   40.2 0.1 -1.9   38.5 1.3 -1.3   40.4 1.1 -1.7
  2007 39.8 0.9   42.1 0.6   41.5 1.8 1.7   43.8 1.9 1.7   37.8 0.5 -2.0   40.1 0.0 -2.0   38.6 1.4 -1.2   40.5 1.2 -1.6
  2008 39.9 1.0   42.1 0.6   41.7 2.0 1.8   44.0 2.1 1.9   37.8 0.5 -2.1   40.1 0.0 -2.0   38.7 1.5 -1.2   40.5 1.2 -1.6
3rd period              
  2009 39.9 1.0   42.1 0.6   41.9 2.2 2.0   44.2 2.3 2.1   37.8 0.5 -2.1   40.0 -0.1 -2.1   38.8 1.6 -1.1   40.6 1.3 -1.5
  2010 40.0 1.1   42.1 0.6   42.1 2.4 2.1   44.4 2.5 2.3   37.8 0.5 -2.2   40.0 -0.1 -2.1   38.9 1.7 -1.1   40.6 1.3 -1.5
  2011 40.1 1.2   42.2 0.7   42.3 2.6 2.2   44.5 2.6 2.3   37.9 0.6 -2.2   39.9 -0.2 -2.3   39.0 1.8 -1.1   40.7 1.4 -1.5
  2012 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.6 2.7 2.4   37.9 0.6 -2.3   39.9 -0.2 -2.3   39.1 1.9 -1.1   40.8 1.5 -1.4
  2013 40.2 1.3   42.2 0.7   42.4 2.7 2.2   44.7 2.8 2.5   38.0 0.7 -2.2   39.9 -0.2 -2.3   39.2 2.0 -1.0   40.9 1.6 -1.3
Δint correspnds to the change since start

Δstd corresponds to the change compared to national average

The ztable package

The package can also export to \LaTeX.

Example 1

library(ztable)

options(ztable.type='html')

# given the data in the first row
zt <- ztable(out_mx, 
             caption = 'Average age in Sweden counties over a period of
             15 years. The Norbotten county is typically known
             for having a negative migration pattern compared to
             Stockholm, while Uppsala has a proportionally large 
             population of students.',
             zebra.type = 1,
             zebra = 'peach',
             align=paste(rep('r', ncol(out_mx) + 1), collapse = ''))
# zt <- addcgroup(zt,
#                 cgroup = cgroup,
#                 n.cgroup = n.cgroup)
# Causes an error:
# Error in if (result <= length(vlines)) { : 
zt <- addrgroup(zt, 
                rgroup = c('1st&nbsp;period', '2nd&nbsp;period', '3rd&nbsp;period'),
                n.rgroup = rep(5, 3))

print(zt)