How to Create a Frequency Table of Multiple Variables in R


To calculate a frequency table for multiple variables in a data frame in R you can use the apply() function, which uses the following syntax:

apply(X, MARGIN FUN)

where:

  • X: An array, matrix, or data frame
  • MARGIN: Apply a function across rows (1) or columns (2)
  • FUN: The function to be applied

The following examples show how to use this syntax in practice.

Example 1: Frequency Table for All Variables in R

The following code shows how to calculate a frequency table for every variable in a data frame:

#create data frame
df <- data.frame(var1=c(1, 1, 2, 2, 2, 2, 3),
                 var2=c('A', 'A', 'A', 'A', 'B', 'B', 'B'),
                 var3=c(6, 7, 7, 7, 8, 8, 9))

#view first few rows of data frame
head(df)

  var1 var2 var3
1    1    A    6
2    1    A    7
3    2    A    7
4    2    A    7
5    2    B    8
6    2    B    8

#calculate frequency table for every variable in data frame
apply((df), 2, table)

$var1

1 2 3 
2 4 1 

$var2

A B 
4 3 

$var3

6 7 8 9 
1 3 2 1

The result is three frequency tables – one for each variable in the data frame.

Here’s how to interpret the first frequency table:

  • The value 1 appears 2 times in the “var1” column
  • The value 2 appears 4 times in the “var2” column
  • The value 3 appears 1 time in the “var3” column

The other frequency tables can be interpreted in a similar manner.

Example 2: Frequency Table for Specific Variables in R

The following code shows how to calculate a frequency table for specific variables in a data frame

#create data frame
df <- data.frame(var1=c(1, 1, 2, 2, 2, 2, 3),
                 var2=c('A', 'A', 'A', 'A', 'B', 'B', 'B'),
                 var3=c(6, 7, 7, 7, 8, 8, 9))

#calculate frequency table for var1 and var3 columns
apply((df[c('var1', 'var3')]), 2, table)

$var1

1 2 3 
2 4 1

$var3

6 7 8 9 
1 3 2 1

Example 3: Frequency Table for All But One Variable in R

Suppose we have an index column in a data frame and we would like to calculate a frequency table for every variable in the data frame except the index column.

The following code shows how to do so:

#create data frame
df <- data.frame(index=c(1, 2, 3, 4, 5, 6, 7),
                 var2=c('A', 'A', 'A', 'A', 'B', 'B', 'B'),
                 var3=c(6, 7, 7, 7, 8, 8, 9))

#calculate frequency table for all columns except index column
apply((df[-1]), 2, table)

$var2

A B 
4 3 

$var3

6 7 8 9 
1 3 2 1

Additional Resources

How to Create a Two Way Table in R
How to Create a Relative Frequency Histogram in R

Leave a Reply

Your email address will not be published.