How can I create a frequency table by group in R?

How can I create a frequency table by group in R?

Creating a frequency table by group in R involves organizing data into categories and counting the number of occurrences within each category. This can be done by using the “table” function in R, which allows for the creation of a table that displays the frequencies of each group. The table can then be further customized by adding labels and formatting options. This method is useful for summarizing and analyzing large datasets, as it provides a comprehensive overview of the distribution of data within different groups.

Create a Frequency Table by Group in R


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:

Cite this article

stats writer (2024). How can I create a frequency table by group in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-frequency-table-by-group-in-r/

stats writer. "How can I create a frequency table by group in R?." PSYCHOLOGICAL SCALES, 6 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-frequency-table-by-group-in-r/.

stats writer. "How can I create a frequency table by group in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-frequency-table-by-group-in-r/.

stats writer (2024) 'How can I create a frequency table by group in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-frequency-table-by-group-in-r/.

[1] stats writer, "How can I create a frequency table by group in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I create a frequency table by group in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top