Cumulative frequency and probability tables help summarize data distributions. They show how data values accumulate over time or categories and allow us to calculate probabilities from frequency distributions.
Functions Used
- Table(): Used to create a frequency table by counting occurrences of each unique value in a vector.
frequency_table <- table (vec)
Parameter:
- vec: A vector or factor containing the data to be counted.
- Cumsum():Computes cumulative frequency by summing the frequencies progressively across the table.
cumsum ( frequency_table)
Parameter:
- frequency_table: A table of frequencies generated from the table() function.
- prop.table: Converts the frequency table into a probability table by dividing each frequency by the total count.
prop.table(frequency_table)
Parameter:
- frequency_table: A table of frequencies generated from the table() function.
Example 1: We are creating a frequency table and a cumulative frequency table from a sample of categorical values.
- set.seed: Ensures the random sample is reproducible.
- vec: A character vector containing randomly sampled values.
- sample: Randomly selects elements from a specified set.
- table: Generates a frequency table of unique values in the vector.
- data: Stores the frequency table created from
vec. - print: Displays the output to the console.
- cumsum: Calculates the cumulative frequency from the frequency table.
- cumfreq_data: Stores the cumulative frequency result.
set.seed(1)
vec <- sample(c("Geeks", "CSE", "R", "Python"), 50, replace = TRUE)
data <- table(vec)
print("Frequency Table")
print(data)
print("Cumulative Frequency Table")
cumfreq_data <- cumsum(data)
print(cumfreq_data)
Output:

Example 2: We are creating a probability table by dividing the frequency counts by the total number of observations in the sample.
- prob_data: Stores the probability table obtained by dividing the frequency table by 50.
set.seed(1)
vec <- sample(c("Geeks", "CSE", "R", "Python"), 50, replace = TRUE)
data <- table(vec)
print("Frequency Table")
print(data)
print("Probability Table")
prob_data <- data / 50
print(prob_data)
Output:

Example 3: We are creating a data frame that combines frequency, cumulative frequency and probability values into one structured table.
- num_obsrv: Represents the total number of observations (50).
- prob_data: Stores probabilities calculated by dividing frequency by total observations.
- cumsum: Computes the cumulative frequency from the frequency table.
- cumfreq_data: Stores the cumulative frequency values.
- colnames: Assigns custom names to the columns of the data frame.
set.seed(1)
vec <- sample(c("Geeks", "CSE", "R", "Python"), 50, replace = TRUE)
data <- table(vec)
num_obsrv <- 50
prob_data <- data / num_obsrv
cumfreq_data <- cumsum(data)
data_frame <- data.frame(data, cumfreq_data, prob_data)
colnames(data_frame) <- c("data", "frequency", "cumulative_frequency", "data", "probability")
print(data_frame)
Output:

The output displays a data frame showing each category along with its frequency, cumulative frequency and probability.