Table of Contents
The table function in R is an efficient way to summarize data. It takes input in the form of vectors, factors and/or other objects and creates a frequency table that displays the number of occurrences of each unique value in the vector. With the table function, users can quickly summarize data and understand the distribution of different variables. Examples of the table function in action can be found on the R documentation website.
The table() function in R can be used to quickly create frequency tables.
This tutorial provides examples of how to use this function with the following data frame in R:
#create data frame df <- data.frame(player = c('AJ', 'Bob', 'Chad', 'Dan', 'Eric', 'Frank'), position = c('A', 'B', 'B', 'B', 'B', 'A'), points = c(1, 2, 2, 1, 0, 0)) #view data frame df player position points 1 AJ A 1 2 Bob B 2 3 Chad B 2 4 Dan B 1 5 Eric B 0 6 Frank A 0
Example 1: Frequency Table for One Variable
The following code shows how to create a frequency table for the position variable in our data frame:
#calculate frequency table for position variable
table(df$position)
A B
2 4
From the output we can observe:
- 2 players in the data frame have a position of ‘A‘
- 4 players in the data frame have a position of ‘B‘
Example 2: Frequency Table of Proportions for One Variable
The following code shows how to use prop.table() to create a frequency table of proportions for the position variable in our data frame:
#calculate frequency table of proportions for position variable prop.table(table(df$position)) A B 0.3333333 0.6666667
From the output we can observe:
- 33.33% of players in the data frame have a position of ‘A‘
- 66.67% of players in the data frame have a position of ‘B‘
Note that in a proportion table the sum of the proportions will always be equal to 1.
Example 3: Frequency Table for Two Variables
The following code shows how to create a frequency table for the position and points variable in our data frame:
#calculate frequency table for position and points variable
table(df$position, df$points)
0 1 2
A 1 1 0
B 1 1 2
- 1 player in the data frame has a position of ‘A‘ and 0 points
- 1 player in the data frame has a position of ‘A‘ and 1 point
- 0 players in the data frame have a position of ‘A‘ and 2 points
- 1 player in the data frame has a position of ‘B‘ and 0 points
- 1 player in the data frame has a position of ‘B‘ and 1 point
- 2 players in the data frame have a position of ‘B‘ and 2 points
Example 4: Frequency Table of Proportions for Two Variables
The following code shows how to create a frequency table of proportions for the position and points variable in our data frame:
#calculate frequency table of proportions for position and points variable prop.table(table(df$position, df$points)) 0 1 2 A 0.1666667 0.1666667 0.0000000 B 0.1666667 0.1666667 0.3333333
From the output we can observe:
- 16.67% of players in the data frame have a position of ‘A‘ and 0 points
- 16.67% of players in the data frame have a position of ‘A‘ and 1 point
- 0% of players in the data frame have a position of ‘A‘ and 2 points
- 16.67% of players in the data frame have a position of ‘B‘ and 0 points
- 16.67% of players in the data frame have a position of ‘B‘ and 1 point
- 33.3% of players in the data frame have a position of ‘B‘ and 2 points
Note that we can also use the options() function to specify how many decimals to show in the proportion table:
#only display two decimal places options(digits=2) #calculate frequency table of proportions for position and points variable prop.table(table(df$position, df$points)) 0 1 2 A 0.17 0.17 0.00 B 0.17 0.17 0.33