Edit: This post originally appeared on my WordPress blog on September 21, 2009. I present it here in its original form.
The R Function of the Day series will focus on describing in plain language how certain R functions work, focusing on simple examples that you can apply to gain insight into your own data.
Today, I will discuss the table function.
What situation is table useful in?
The table function is a very basic, but essential, function to master while performing interactive data analyses. It simply creates tabular results of categorical variables. However, when combined with the powers of logical expressions in R, you can gain even more insights into your data, including identifying potential problems.
We want to know how many subjects are enrolled at each center in a clinical trial.
We want to know how many subjects are under the age of 60 in a clinical trial.
Which center has the most subjects with a missing value for age in the clinical trial?
How do I use table?
The table function simply needs an object that can be interpreted as a categorical variable (called a “factor” in R).
> ## generate data for medical example > clinical.trial <- data.frame(patient = 1:100, age = rnorm(100, mean = 60, sd = 6), treatment = gl(2, 50, labels = c("Treatment", "Control")), center = sample(paste("Center", LETTERS[1:5]), 100, replace = TRUE)) > ## set some ages to NA (missing) > is.na(clinical.trial$age) <- sample(1:100, 20) > summary(clinical.trial) patient age treatment center Min. : 1.00 Min. :46.61 Treatment:50 Center A:22 1st Qu.: 25.75 1st Qu.:56.19 Control :50 Center B:10 Median : 50.50 Median :60.59 Center C:28 Mean : 50.50 Mean :60.57 Center D:23 3rd Qu.: 75.25 3rd Qu.:64.84 Center E:17 Max. :100.00 Max. :77.83 NA's :20.00
Example 1 is the most trivial. We want to know how many subjects are enrolled at each center, so simply pass in the variable “center” to the table function.
> ## a simple example of a table call > table(clinical.trial$center) Center A Center B Center C Center D Center E 22 10 28 23 17
For example 2, we need to create a logical vector indicating whether or not a patient is under 60 or not. We can then pass that into the table function. Also, since there are missing ages, we might be interested in seeing those in the table also. It is shown both ways by setting the “useNA” argument to table.
> ## a logical vector is created and passed into table > table(clinical.trial$age < 60) FALSE TRUE 41 39 > ## the useNA argument shows the missing values, too > table(clinical.trial$age < 60, useNA = "always") FALSE TRUE <NA> 41 39 20
Example 3, finding the center that has the most missing values for age, sounds the trickiest, but is once again an extremely simple task with the table function. You just need to know that the is.na function returns a logical vector that indicates whether an observation is missing or not.
> ## the table of missing age by center > table(clinical.trial$center, is.na(clinical.trial$age)) FALSE TRUE Center A 16 6 Center B 8 2 Center C 23 5 Center D 20 3 Center E 13 4 > ## centers with most missing ages listed in order > ## highest to lowest > sort(table(clinical.trial$center, is.na(clinical.trial$age))[, 2], decreasing = TRUE) Center A Center C Center E Center D Center B 6 5 4 3 2
Summary of table
Although table is an extremely simple function, its use should not be avoided when exploring a dataset. These examples have shown you how to use table on variables in a dataset, and on variables created from a logical expression in R. The “useNA” argument was also introduced.