đ 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
đ¯ 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
đ 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.
| Measure | Description | R Function |
|---|---|---|
| Mean | Arithmetic average. | mean() |
| Median | Middle value after sorting. | median() |
| Mode | Most 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.
| Measure | Description | Function |
|---|---|---|
| Range | Difference between maximum and minimum values. | range() |
| Variance | Average squared deviation from the mean. | var() |
| Standard Deviation | Square 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
đ Common Statistical Functions
| Function | Description |
|---|---|
| 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
| Mistake | Explanation | Solution |
|---|---|---|
| Ignoring missing values | Statistical functions may return NA. | Use na.rm = TRUE when appropriate. |
| Confusing mean and median | Mean is sensitive to outliers, while the median is more robust. | Choose the measure that best represents the data. |
| Expecting a built-in mode function | R does not provide one for statistical mode. | Create a custom function or use an external package. |
| Interpreting summary statistics without visualizing the data | Important 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
đ 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.