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.

Example 1

We want to know how many subjects are enrolled at each center in a clinical trial.

Example 2

We want to know how many subjects are under the age of 60 in a clinical trial.

Example 3

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).

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.

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.

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.