Here are two ways to calculate relative frequencies in R:

- Using
**dplyr**package - Using
**frequency tables**

**Method 1: Using the dplyr package**

- 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, 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**

The resulting dataframe will contain each unique combination of ** Gender** and

**along with its corresponding relative frequency.**

`Age`

**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 the **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**

**Method 2: Using frequency tables**

The **table()** function calculates the frequency of each unique value in the dataset, and dividing these frequencies by the total number of values in the dataset (as obtained by length(data)) converts these into relative frequencies.

Each relative frequency represents the proportion of times a particular value appears in the dataset.

**Example 1: Frequency table for one data frame column**

```
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)
)
# Calculate relative frequency table for 'Gender' column
table(df$Gender) / length(df$Gender)
```

**Output**

```
Female Male
0.4285714 0.5714286
```

**Example 2: Calculating for**** all data frame columns**

```
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)
)
# calculate relative frequency table for each column
sapply(df, function(x) table(x) / nrow(df))
```

**Output**

That’s all!

**Related posts**

Find the Maximum Value By Group

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.