- You are here:
- Home
- Blog
- Getting Started
- Table Function in R

The simplest way to depict a table is by creating a two way table of occurrences using the table command and the two columns in the data frame

table(Data$col1,Data$col2)

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.

Table uses the cross-classifying factors to build a contingency table of the counts at each combination of factor levels. table() returns a contingency table, an object of class “table”, an array of integer values.

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

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.

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

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.