# Tabulation

R can produce tables either of aggregate data or summary statistics. The most useful initial tables are raw counts +/- percentages, either of a single variable, with respect to two variables, or by groupings.

First we generate some data. The following code simulates the features of 100 patients in a clinical trial, including their ages, the centre they attended (A to E), the treatment they received (intervention or control).

``````
Input:
clinical_trial <-
data.frame(patient = 1:100,
age = rnorm(100, mean = 60, sd = 6),
treatment = gl(2, 50,
labels = c("Treatment", "Control")),
centre = sample(paste("Centre", LETTERS[1:5]),
100, replace = TRUE))

attach(clinical_trial)

clinical_trial[1:10, ]

Output:
patient	age	        treatment	centre
1	53.26911	Treatment	Centre E
2	72.55279	Treatment	Centre A
3	61.86786	Treatment	Centre E
4	59.87694	Treatment	Centre A
5	56.48190	Treatment	Centre E
6	53.63273	Treatment	Centre A
7	54.70294	Treatment	Centre A
8	69.42797	Treatment	Centre A
9	54.08574	Treatment	Centre E
10	47.69731	Treatment	Centre B

``````
``` ```

The simplest table we can create is a one way table of counts, e.g. based on whether patients received the intervention or the control treatment. The `table()` function produces a count of the specified column.

``````
Input:
table(treatment)

Output:
treatment
Treatment   Control
50        50
``````
``` ```

To create a twoway table, simply list the variables in the preferred order. The 'dnn' option creates column and row variable labels for the table.

``````
Input:
table(treatment, centre, dnn=c("TREATMENTS", "CENTRES"))

Output:
CENTRES
TREATMENTS  Centre A Centre B Centre C Centre D Centre E
Treatment       11        6        7       10       16
Control         11       16        7        9        7
``````
``` ```

Sometimes, we may wish to create an entirely new dataframe from the table.

``````
Input:
centres<-as.data.frame(table(treatment, centre),
responseName="counts")
centres

Output:
treatment	centre	counts
Treatment	Centre A	11
Control	Centre A	11
Treatment	Centre B	6
Control	Centre B	16
Treatment	Centre C	7
Control	Centre C	7
Treatment	Centre D	10
Control	Centre D	9
Treatment	Centre E	16
Control	Centre E	7

``````
``` ```

For more advanced options, see the table() documentation online.