# calculate the mean, median, and mode using R.
# Creating a discrete grouped data (Product Prices and Sales Frequency)
Product_Prices <- c(50, 100, 150, 200, 250) # Product price ranges
Sales_Frequency <- c(500, 700, 600, 800, 400) # Number of products sold in each price range
# Finding the mean for this discrete data
discrete_mean <- function(v, f){
total_weighted_values <- sum(v * f)
total_frequencies <- sum(f)
mean <- total_weighted_values / total_frequencies
cat("The mean of the given data is:", mean, "\n")
}
# Finding the mode for this discrete data
discrete_mode <- function(v, f){
mode_value <- v[which.max(f)]
cat("The mode for the discrete frequency distribution is:", mode_value, "\n")
}
# Finding the median for this discrete data
discrete_median <- function(v, f){
# Calculating the cumulative frequency
cf <- cumsum(f)
# Calculating total frequency
N <- sum(f)
median_position <- (N + 1) / 2
# Finding the relevant position
median_index <- which(cf >= median_position)[1]
median <- v[median_index]
cat("The median of the given data is:", median, "\n")
}
# Displaying the data
cat("Product Prices:", Product_Prices, "\n")
## Product Prices: 50 100 150 200 250
cat("Sales Frequency:", Sales_Frequency, "\n")
## Sales Frequency: 500 700 600 800 400
# Calculating and displaying central tendency measures
discrete_mean(Product_Prices, Sales_Frequency)
## The mean of the given data is: 148.3333
discrete_mode(Product_Prices, Sales_Frequency)
## The mode for the discrete frequency distribution is: 200
discrete_median(Product_Prices, Sales_Frequency)
## The median of the given data is: 150
#2)Create your own discrete data set and
# calculate the mean, median, and mode using R.
# Define the temperature ranges (in Celsius) and frequency counts
temperature_ranges <- list(c(-10, 0), c(0, 10), c(10, 20), c(20, 30), c(30, 40), c(40, 50))
temperature_counts <- c(15, 25, 35, 30, 20, 10)
# Set seed for reproducibility
set.seed(42)
# Generate continuous temperature data by randomly sampling within each range
continuous_data <- unlist(mapply(function(range, count) runif(count, range[1], range[2]),
temperature_ranges, temperature_counts))
# Convert to data frame
df <- data.frame(Temperature = continuous_data)
# Display summary statistics
summary(df)
## Temperature
## Min. :-8.653
## 1st Qu.: 9.049
## Median :17.847
## Mean :18.752
## 3rd Qu.:27.599
## Max. :49.626
# Create a bar graph for class intervals vs. frequency
barplot(temperature_counts, names.arg = c("-10 to 0", "0 to 10", "10 to 20", "20 to 30", "30 to 40", "40 to 50"),
col = "green", main = "Temperature Ranges vs. Frequency",
xlab = "Temperature Range (°C)", ylab = "Frequency", border = "black")
# 3)Create your own continuous data set and
# calculate the mean, median, and mode using R.
# Finding the measure of central tendencies for the continuous grouped data
Salary_range <- c("20000-40000", "40000-60000", "60000-80000", "80000-100000", "100000-120000", "120000-140000")
Employees <- c(45, 30, 20, 15, 10, 5)
class_width <- 20000
# Defining lower and upper limits
lower_limits <- c(20000, 40000, 60000, 80000, 100000, 120000)
upper_limits <- c(40000, 60000, 80000, 100000, 120000, 140000)
midpoints <- (lower_limits + upper_limits) / 2
# Calculating the mean
continuous_mean <- sum(Employees * midpoints) / sum(Employees)
cat("The mean of the given salary data is:", continuous_mean, "\n")
## The mean of the given salary data is: 58800
# Function to calculate the mode
continuous_mode <- function(lower_limits, Employees, class_width) {
modal_index <- which.max(Employees)
l <- lower_limits[modal_index]
f1 <- Employees[modal_index]
f0 <- ifelse(modal_index > 1, Employees[modal_index - 1], 0)
f2 <- ifelse(modal_index < length(Employees), Employees[modal_index + 1], 0)
# Applying the mode formula
mode <- l + ((f1 - f0) / (2 * f1 - f0 - f2)) * class_width
cat("The mode for the continuous salary data is:", mode, "\n")
}
# Function to calculate the median
continuous_median <- function(lower_limits, Employees, class_interval) {
cf <- cumsum(Employees)
N <- sum(Employees)
median_position <- N / 2
median_class_index <- which(cf >= median_position)[1]
l <- lower_limits[median_class_index]
f <- Employees[median_class_index]
F <- ifelse(median_class_index == 1, 0, cf[median_class_index - 1])
c <- class_interval
# Applying the median formula
median <- l + ((median_position - F) / f) * c
cat("The median for the continuous salary data is:", median, "\n")
}
# Calculating and displaying the mode and median
continuous_mode(lower_limits, Employees, class_width)
## The mode for the continuous salary data is: 35000
continuous_median(lower_limits, Employees, class_width)
## The median for the continuous salary data is: 51666.67
# Displaying the data
cat("Salary Ranges:", Salary_range, "\n")
## Salary Ranges: 20000-40000 40000-60000 60000-80000 80000-100000 100000-120000 120000-140000