R Program to Calculate Mean, Median, and Mode

Introduction

Calculating the mean, median, and mode are fundamental statistical operations used to summarize a dataset. The mean provides the average of the data, the median gives the middle value when the data is sorted, and the mode identifies the most frequent value in the dataset. This guide will walk you through writing an R program to calculate the mean, median, and mode of a given set of data.

Problem Statement

Create an R program that:

  • Defines a set of numeric data.
  • Calculates the mean, median, and mode of the data.
  • Displays the calculated values.

Example:

  • Input: A vector of numeric values: 34, 23, 55, 45, 67, 89, 70, 56, 78, 34, 45, 67, 89, 23
  • Output: The mean, median, and mode of the data.

Solution Steps

  1. Define the Data: Use a vector to store the numeric values.
  2. Calculate the Mean: Use the mean() function to calculate the average.
  3. Calculate the Median: Use the median() function to calculate the middle value.
  4. Calculate the Mode: Write a custom function to calculate the mode since R does not have a built-in mode function.
  5. Display the Results: Use the print() function to display the mean, median, and mode.

R Program

# R Program to Calculate Mean, Median, and Mode # Step 1: Define the data data <- c(34, 23, 55, 45, 67, 89, 70, 56, 78, 34, 45, 67, 89, 23) # Step 2: Calculate the mean mean_value <- mean(data) # Step 3: Calculate the median median_value <- median(data) # Step 4: Calculate the mode calculate_mode <- function(x) { uniq_values <- unique(x) uniq_values[which.max(tabulate(match(x, uniq_values)))] } mode_value <- calculate_mode(data) # Step 5: Display the results print(paste("Mean:", mean_value)) print(paste("Median:", median_value)) print(paste("Mode:", mode_value)) 

Explanation

Step 1: Define the Data

  • A vector data is created to store the numeric values that will be analyzed.

Step 2: Calculate the Mean

  • The mean() function is used to calculate the mean (average) of the data, and the result is stored in mean_value.

Step 3: Calculate the Median

  • The median() function is used to calculate the median (the middle value) of the data, and the result is stored in median_value.

Step 4: Calculate the Mode

  • Since R does not have a built-in mode function, a custom function calculate_mode() is defined:
    • The function finds the unique values in the data.
    • It then counts the occurrences of each unique value using tabulate().
    • Finally, it returns the value with the maximum frequency.
  • The mode is calculated by calling calculate_mode(data), and the result is stored in mode_value.

Step 5: Display the Results

  • The print() function is used to display the calculated mean, median, and mode.

Output Example

Example Output:

[1] "Mean: 56.7857142857143" [1] "Median: 56" [1] "Mode: 34" 
  • Mean: The average of the data is approximately 56.79.
  • Median: The middle value of the data is 56.
  • Mode: The most frequent value in the data is 34.

Conclusion

This R program demonstrates how to calculate the mean, median, and mode of a dataset. It covers essential statistical operations and provides a custom function to calculate the mode. These summary statistics are crucial for understanding the central tendency of a dataset, making this example valuable for anyone learning R programming and basic statistical analysis.

Leave a Comment

Scroll to Top