Calculating Descriptive Statistics in R 1) Mean: The average of the numbers. mean(data) 2) Median: The middle value. median(data) 3) Mode : The most frequent value (not directly available in R, so we create a custom function). mode_value <- as.numeric (names(which.max(table(data)))) mode_value
4) Range: The difference between the maximum and minimum values. range(data) 5) Standard Deviation: Tells us how spread out the data is. sd(data) 6) Frequency: The count of each unique value. table(data)
Creating Visualizations in R 1. Histogram A histogram shows the distribution of a numeric variable. # Create a histogram hist (data, main="Histogram of Data", xlab ="Values", col =" lightblue ", breaks=5) Explanation: The histogram groups data into ranges (bins) and shows how many data points fall into each range. 2. Bar Chart A bar chart is used for categorical data. # Create a bar chart for categories barplot (table(categories), main="Bar Chart of Categories", col =" lightgreen ", ylab ="Frequency”) Explanation: This bar chart shows the frequency of each category.
Case Study: Analyzing Sales Data of a Supermarket Chain Background: A supermarket chain wants to understand its sales performance to make better business decisions. The dataset contains information on total sales, product categories, regions, and the number of stores. The company wants to analyze key performance indicators (KPIs) such as average sales, variability in sales across different regions, and distribution of product categories.
Objectives: Calculate the average sales across all stores. Determine the variability (standard deviation) in sales. Analyze the distribution of sales by product category. Understand regional sales performance using summary statistics.
Dataset Structure: Region: Region of the store (North, South, East, West) Store: Store ID Product_Category: Category of products sold (e.g., Beverages, Snacks, Produce, Dairy) Sales: Total sales in dollars for the period
Region Store Product-Category Sales North 101 BEVERAGES 5000 South 102 DAIRY 4200 East 103 SNACKS 3200 West 104 PRODUCTS 2800 North 105 BEVERAGES 4800