13.2 C
London
Tuesday, July 2, 2024
HomeRDescriptive Statistics in RHow to Create Relative Frequency Tables in R

How to Create Relative Frequency Tables in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

A relative frequency table tells you how often certain values in a dataset occur relative to the total number of values in the dataset.

You can use the following basic syntax to create a frequency table in R:

table(data)/length(data)

The table() function calculates the frequency of each individual data value and the length() function calculates the total number of values in the dataset.

Thus, dividing each individual frequency by the length of the dataset gives us the relative frequency of each value in the dataset.

The following examples show how to use this syntax in practice.

Example 1: Relative Frequency Table for One Vector

The following code shows how to create a relative frequency table for a single vector in R:

#define data
data #create relative frequency table
table(data)/length(data)

  A   B   C 
0.2 0.3 0.5 

Here’s how to interpret the table:

  • 20% of all values in the dataset are the letter A
  • 30% of all values in the dataset are the letter B
  • 50% of all values in the dataset are the letter C

Example 2: Relative Frequency Table for One Data Frame Column

The following code shows how to create a relative frequency table for one column of a data frame in R:

#define data frame
df frame(team=c('A', 'A', 'A', 'A', 'A', 'B', 'B', 'C'),
                 wins=c(2, 9, 11, 12, 15, 17, 18, 19),
                 points=c(1, 2, 2, 2, 3, 3, 3, 3))

#view first few rows of data frame
head(df)

  team wins points
1    A    2      1
2    A    9      2
3    A   11      2
4    A   12      2
5    A   15      3
6    B   17      3

#calculate relative frequency table for 'team' column
table(df$team)/length(df$team)
 
    A      B      C 
0.625  0.250  0.125

Example 3: Relative Frequency Table for All Data Frame Columns

The following code shows how to create a relative frequency table for every column of a data frame in R:

#define data frame
df frame(team=c('A', 'A', 'A', 'A', 'A', 'B', 'B', 'C'),
                 wins=c(2, 9, 11, 12, 15, 17, 18, 19),
                 points=c(1, 2, 2, 2, 3, 3, 3, 3))

#calculate relative frequency table for each column
sapply(df, function(x) table(x)/nrow(df))

$team
x
    A     B     C 
0.625 0.250 0.125 

$wins
x
    2     9    11    12    15    17    18    19 
0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125 

$points
x
    1     2     3 
0.125 0.375 0.500 

Additional Resources

How to Create Frequency Tables in R
How to Create a Frequency Table of Multiple Variables in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories