Descriptive Statistics in R

📘 Introduction

Descriptive statistics is the branch of statistics that summarizes, organizes, and describes the main characteristics of a dataset. Instead of making predictions or drawing conclusions about a population, descriptive statistics focuses on presenting data in a meaningful and understandable way using numerical measures and graphical summaries.

Information

R provides numerous built-in functions for calculating descriptive statistics, making it one of the most widely used tools for data analysis and statistical computing.

đŸŽ¯ Why Learn Descriptive Statistics?

  • Understand the characteristics of a dataset.
  • Summarize large amounts of data efficiently.
  • Identify trends, patterns, and outliers.
  • Support decision-making using data.
  • Prepare data for advanced statistical analysis.

📊 Types of Descriptive Statistics

Descriptive Statistics
Measures of Central Tendency
Measures of Dispersion
Measures of Position
Frequency Distribution
Graphical Summaries

📝 Sample Dataset

Student Marks

marks <- c(
  78, 85, 92, 88, 76,
  95, 81, 89, 84, 90
)

print(marks)

📍 Measures of Central Tendency

Measures of central tendency describe the center or typical value of a dataset.

MeasureDescriptionR Function
MeanArithmetic average.mean()
MedianMiddle value after sorting.median()
ModeMost frequently occurring value.No built-in function.

Calculating Mean and Median

mean(marks)

median(marks)

Output

Console Output

[1] 85.8
[1] 86.5

📍 Calculating the Mode

R does not include a built-in function for calculating the statistical mode, but it can be created using the following function.

Mode Function

getMode <- function(x) {
  uniqueValues <- unique(x)

  uniqueValues[
    which.max(
      tabulate(
        match(x, uniqueValues)
      )
    )
  ]
}

numbers <- c(
  2,4,4,6,7,4,8
)

getMode(numbers)

Output

Console Output

[1] 4

📏 Measures of Dispersion

Measures of dispersion describe how spread out the data values are.

MeasureDescriptionFunction
RangeDifference between maximum and minimum values.range()
VarianceAverage squared deviation from the mean.var()
Standard DeviationSquare root of variance.sd()

Dispersion Measures

range(marks)

var(marks)

sd(marks)

📈 Finding Minimum and Maximum

Minimum and Maximum

min(marks)

max(marks)

📊 Quartiles and Percentiles

Quartiles divide data into four equal parts and help describe the distribution.

Quartiles

quantile(marks)

Output

Console Output

0%   25%   50%   75%  100%
76.0 81.75 86.50 89.75 95.0

📋 Summary Statistics

The summary() function provides a quick statistical overview of numerical data.

Using summary()

summary(marks)

Output

Console Output

Min.   :76.00
1st Qu.:81.75
Median :86.50
Mean   :85.80
3rd Qu.:89.75
Max.   :95.00

📊 Frequency Distribution

Frequency tables show how often each value occurs.

Frequency Table

grades <- c(
  "A","B","A","C",
  "B","A","B","A"
)

table(grades)

Output

Console Output

grades
A B C
4 3 1

📉 Cumulative Frequency

Cumulative Frequency

freq <- table(grades)

cumsum(freq)

Output

Console Output

A B C
4 7 8

📊 Descriptive Statistics for Data Frames

Student Dataset

students <- data.frame(
  Name = c(
    "Alice",
    "Bob",
    "Charlie",
    "David"
  ),
  Marks = c(
    85,
    72,
    91,
    78
  ),
  Age = c(
    20,
    21,
    20,
    22
  )
)

summary(students)

📈 Applying Statistics to Multiple Columns

Use the apply() function to calculate statistics across multiple columns.

Column Means

scores <- data.frame(
  Math = c(80,90,85),
  Science = c(75,88,92),
  English = c(78,81,89)
)

apply(
  scores,
  2,
  mean
)

đŸ“Ļ Using Built-in Datasets

R includes several built-in datasets for learning and analysis.

Summary of iris Dataset

summary(iris)

mean(iris$Sepal.Length)

sd(iris$Sepal.Length)

📊 Graphical Summaries

Graphs provide visual summaries of descriptive statistics.

Histogram

hist(
  marks,
  col = "skyblue",
  main = "Distribution of Marks"
)

Box Plot

boxplot(
  marks,
  col = "orange",
  main = "Student Marks"
)

🌍 Real-World Example

A company wants to analyze monthly employee salaries to understand the overall salary distribution.

Employee Salary Analysis

salary <- c(
  42000,
  50000,
  47000,
  61000,
  58000,
  52000,
  49000
)

summary(salary)

mean(salary)

median(salary)

sd(salary)

range(salary)

🔄 Descriptive Statistics Workflow

Collect Data
Clean Data
Calculate Statistics
Measure Center
Measure Spread
Create Graphs
Interpret Results

📋 Common Statistical Functions

FunctionDescription
mean()Calculates the average.
median()Returns the middle value.
min()Returns the smallest value.
max()Returns the largest value.
range()Returns minimum and maximum values.
var()Calculates variance.
sd()Calculates standard deviation.
quantile()Calculates quantiles.
summary()Displays summary statistics.
table()Creates frequency tables.

âš ī¸ Common Mistakes

MistakeExplanationSolution
Ignoring missing valuesStatistical functions may return NA.Use na.rm = TRUE when appropriate.
Confusing mean and medianMean is sensitive to outliers, while the median is more robust.Choose the measure that best represents the data.
Expecting a built-in mode functionR does not provide one for statistical mode.Create a custom function or use an external package.
Interpreting summary statistics without visualizing the dataImportant patterns and outliers may be missed.Use charts such as histograms and box plots alongside numerical summaries.

💡 Best Practices

  • Inspect data before performing statistical analysis.
  • Handle missing values appropriately.
  • Use both numerical summaries and graphical visualizations.
  • Choose statistical measures based on the characteristics of the dataset.
  • Document assumptions and analysis steps for reproducibility.

Best Practice

Descriptive statistics provide the foundation for understanding any dataset. Combining summary measures with visualizations helps reveal patterns, detect anomalies, and prepare data for more advanced statistical analyses.

📝 Summary

Descriptive statistics summarize and describe the key characteristics of a dataset using measures of central tendency, dispersion, position, and frequency. In this chapter, you learned how to calculate the mean, median, mode, range, variance, standard deviation, quartiles, and frequency distributions using Base R functions. You also explored summary statistics, graphical summaries, and descriptive analysis for data frames and built-in datasets. These techniques form the basis for effective data exploration and statistical analysis in R.

>>"Descriptive statistics turn raw data into meaningful information, providing the first step toward understanding and making informed decisions."