7 Tables

7.1 Displaying tables - htmlTable

htmlTable is a package that will produce the HTML code needed to display a table in a browser.

agegp alcgp tobgp ncases ncontrols
1 25-34 0-39g/day 0-9g/day 0 40
2 25-34 0-39g/day 10-19 0 10
3 25-34 0-39g/day 20-29 0 6
4 25-34 0-39g/day 30+ 0 5
5 25-34 40-79 0-9g/day 0 27

There are various options to change how a htmlTable will look:

Oesophageal Cancer
Age Group Alcohol Group Tobacco Group ncases ncontrol
25-34 0-39g/day 0-9g/day 0 40
25-34 0-39g/day 10-19 0 10
25-34 0-39g/day 20-29 0 6
25-34 0-39g/day 30+ 0 5
25-34 40-79 0-9g/day 0 27

More css styling is avilable : https://www.w3schools.com/css/css_table.asp

7.2 Displaying Interactive tables - DT

DT is a package that will produce an interactive table in a browser.

## Warning: package 'DT' was built under R version 3.4.4

There are various options and additional format functions to change how a DT table looks:

You might need to convert some columns to factors in order for the filters to work.
For more detail : https://rstudio.github.io/DT/

7.3 Creating Tabulations

Before displaying a table you may need to coerce your data into the right shape. A basic frequency table can be created by using dplyr functions as then displayed with htmlTable:

Cylinders Gears n
4 3 1
4 4 8
4 5 2
6 3 2
6 4 4
6 5 1
8 3 12
8 5 2

When grouping by multiple categories the results will be in a tidy format.
To produce the same reuslts as a 2-way tables, you can use the spread function

Cylinders 3 4 5
4 1 8 2
6 2 4 1
8 12 2

To easily add a row totals use the adorn functions from the janitor package:

Cylinders 3 4 5
4 1 8 2
6 2 4 1
8 12 2
Total 15 12 5

They only way to nest multiple categories along the column dimension, is to create a combined field using the unite function.

Cylinders 3
0
4
0
4
1
5
1
4 1 2 6 2
6 2 2 2 1
8 12 2

An alternative to the above is to reshape data with a cast function. The best version of this function is from the data.table package (it originally comes from the reshape package).

cyl am mpg
length
3
mpg
length
4
mpg
length
5
wt
length
3
wt
length
4
wt
length
5
mpg
median
3
mpg
median
4
mpg
median
5
wt
median
3
wt
median
4
wt
median
5
4 0 1 2 0 1 2 0 21.5 23.6 2.465 3.17
4 1 0 6 2 0 6 2 28.85 28.2 2.0675 1.8265
6 0 2 2 0 2 2 0 19.75 18.5 3.3375 3.44
6 1 0 2 1 0 2 1 21 19.7 2.7475 2.77
8 0 12 0 0 12 0 0 15.2 3.81
8 1 0 0 2 0 0 2 15.4 3.37