How to Create a Frequency Table by Group in R?

To create a frequency table by group in R, you first need to create a data frame containing the data you want to analyze. Then, you can use the table() function to calculate the frequency of each group, and the cbind() function to combine the frequency table with the original data frame. Finally, you can use the aggregate() function to calculate the mean of each group.


You can use the following functions from the package to create a frequency table by group in R:

library(dplyr)

df %>%
  group_by(var1, var2) %>%
  summarize(Freq=n())

The following example shows how to use this syntax in practice.

Example: Create Frequency Table by Group

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 position=c('G', 'G', 'G', 'F', 'G', 'F', 'F', 'C'))

#view data frame
df

  team position
1    A        G
2    A        G
3    A        G
4    A        F
5    B        G
6    B        F
7    B        F
8    B        C

Suppose we’d like to create a frequency table that shows the frequency of each position, grouped by team.

We can use the following syntax to do so:

library(dplyr)

#calculate frequency of position, grouped by team
df %>%
  group_by(team, position) %>%
  summarize(Freq=n())

# A tibble: 5 x 3
# Groups:   team [2]
  team  position  Freq
       
1 A     F            1
2 A     G            3
3 B     C            1
4 B     F            2
5 B     G            1

Here’s how to interpret the output:

  • 1 player on team A has position ‘F’
  • 3 players on team A have position ‘G’
  • 1 player on team B has position ‘C’
  • 2 players on team B have position ‘F’
  • 1 player on team B has position ‘G’

Note that we can rename the column that holds the frequencies by changing the variable name in the summarize() function.

For example, we could rename the column to be named ‘count’ instead:

library(dplyr)

#calculate frequency of position, grouped by team
df %>%
  group_by(team, position) %>%
  summarize(count=n())

# A tibble: 5 x 3
# Groups:   team [2]
  team  position count
       
1 A     F            1
2 A     G            3
3 B     C            1
4 B     F            2
5 B     G            1

The following tutorials explain how to perform other common functions in dplyr:

x