To create relative frequency tables in R, you can use two different ways prop.table() function and dplyr package.

The following methods show how you can do it with syntax.

Method 1: Use prop.table() Function

frequency_table <- table(data)

prop.table(frequency_table)


Method 2: Use dplyr Package

library(dplyr)

df %>%
group_by(data) %>%
summarise(count = n()) %>%
mutate(relative_frequency = count / sum(count))


The following examples show how to create relative frequency table in R.

## Using prop.table() Function

Let’s see how we can use prop.table() function to create relative frequency table in R:

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H","A","B","A","C","D","B","E","H"),
Pressure=c(12,11,12,13,13,9,12,12,9,8,7,8,12,11,9,8),
Status=c("OK","Suspect","OK","OK","Suspect","Suspect","Suspect","OK","OK","OK","OK","OK","Suspect","Suspect","Suspect","OK"))

# Calculate relative frequency
freq_table <- table(df\$Status)

# Calculate relative frequency
prop.table(freq_table)


Output:

     OK Suspect
0.5625  0.4375


Here the output shows relative table for Status column of data frame.

## Using dplyr Package

Let’s see how we can create relative frequency table using dplyr package in R:

library(dplyr)

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H","A","B","A","C","D","B","E","H"),
Pressure=c(12,11,12,13,13,9,12,12,9,8,7,8,12,11,9,8),
Status=c("OK","Suspect","OK","OK","Suspect","Suspect","Suspect","OK","OK","OK","OK","OK","Suspect","Suspect","Suspect","OK"))

# Calculate relative frequency
r <- df %>%
group_by(Machine_name) %>%
summarise(count = n()) %>%
mutate(relative_frequency = count / sum(count))

# Show print relative frequency
print(r)


Output:

  Machine_name count relative_frequency
<chr>        <int>              <dbl>
1 A                3             0.188
2 B                3             0.188
3 C                2             0.125
4 D                2             0.125
5 E                2             0.125
6 F                1             0.0625
7 G                1             0.0625
8 H                2             0.125


Here the output shows relative frequncy table for Machine_name column of data frame.