How to Create a Frequency Table by Group in R


You can use the following functions from the dplyr 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

Additional Resources

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

How to Calculate Relative Frequencies Using dplyr
How to Rank Variables by Group Using dplyr
How to Select Columns by Index Using dplyr

Leave a Reply

Your email address will not be published.