To calculate relative frequencies using the dplyr package in R, you typically follow these steps:
- Count the number of occurrences of each value or group.
- Calculate the sum of all counts to get the total.
- Divide each count by the total to get the relative frequency.
Example 1: Calculating the Relative Frequency of One Variable
library(dplyr)
df <- data.frame(
Age = c(20, 21, 19, 22, 23, 20, 21),
Gender = c("Male", "Female", "Male", "Female", "Male", "Female", "Male"),
Score = c(85, 90, 88, 78, 92, 80, 87)
)
df %>%
group_by(Gender) %>%
summarise(count = n()) %>%
mutate(relative_frequency = count / sum(count))
Output
In this code:
- group_by(Gender) groups the data by Gender.
- summarise(count = n()) counts the number of rows in each gender group.
- mutate(relative_frequency = count/sum (count)) calculates the relative frequency by dividing each count by the total count.
Example 2: Calculating the Relative Frequency of Multiple Variables
To calculate the relative frequency of multiple variables simultaneously using dplyr, you can use the group_by() function with multiple grouping variables followed by the summarise() and mutate() functions.
library(dplyr)
df <- data.frame(
Age = c(20, 21, 19, 22, 23, 20, 21),
Gender = c("Male", "Female", "Male", "Female", "Male", "Female", "Male"),
Score = c(85, 90, 88, 78, 92, 80, 87)
)
df %>%
group_by(Gender, Age) %>%
summarise(count = n()) %>%
mutate(relative_frequency = count / nrow(df))
Output
In this code:
- group_by(Gender, Age) groups the data by Gender and Age.
- summarise(count = n()) counts the number of rows for each combination of Gender and Age.
- mutate(relative_frequency = count / nrow(df)) calculates the relative frequency for each combination by dividing each count by the total number of rows in the dataframe.
The resulting dataframe will contain each unique combination of Gender
and Age
along with its corresponding relative frequency.
Example 3: Displaying Relative Frequencies as Percentages
To display relative frequencies as percentages, you can multiply the relative frequencies by 100. Continuing from the previous example, where we calculated the relative frequencies for both Gender and Age in the dataframe, you can add mutate() function to convert the relative frequencies into percentages:
library(dplyr)
df <- data.frame(
Age = c(20, 21, 19, 22, 23, 20, 21),
Gender = c("Male", "Female", "Male", "Female", "Male", "Female", "Male"),
Score = c(85, 90, 88, 78, 92, 80, 87)
)
df %>%
group_by(Gender, Age) %>%
summarise(count = n()) %>%
mutate(
relative_frequency = count / nrow(df),
percentage = relative_frequency * 100
)
Output
That’s it!
Related posts
Find the Maximum Value By Group in R
Summary Statistics By Group in R

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.