Descriptive Statistics and Probability Distributions
Before modeling anything, you need to summarize and understand your data. This lesson covers descriptive statistics and R's built-in support for probability distributions.
1. Measures of Central Tendency
x <- c(23, 45, 12, 67, 34, 89, 21, 45, 45)
mean(x) # average
median(x) # middle value
table(x) # frequency table (mode is the most frequent value)
2. Measures of Spread
sd(x) # standard deviation
var(x) # variance
range(x) # min and max
IQR(x) # interquartile range
quantile(x) # 0%, 25%, 50%, 75%, 100%
3. summary() — The Fast One-Liner
summary(x)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 12.00 21.00 34.00 42.33 45.00 89.00
summary(df) # works column-by-column on an entire data frame
Pro Tip: summary() is often the very first command to run on any new dataset — it instantly reveals ranges, central tendency, and hints at outliers or skew.
4. Probability Distributions in R
R has four core functions for every distribution, following a consistent naming convention:
| Prefix | Meaning | Example (Normal) |
|---|---|---|
| d | Density (PDF) | dnorm(x, mean, sd) |
| p | Cumulative probability (CDF) | pnorm(q, mean, sd) |
| q | Quantile function (inverse CDF) | qnorm(p, mean, sd) |
| r | Random number generation | rnorm(n, mean, sd) |
5. Working with the Normal Distribution
set.seed(42) # reproducibility
samples <- rnorm(1000, mean = 50, sd = 10)
hist(samples, breaks = 30, col = "skyblue",
main = "Simulated Normal Distribution")
# Probability that a value is less than 60
pnorm(60, mean = 50, sd = 10) # ~0.841
# 95th percentile value
qnorm(0.95, mean = 50, sd = 10) # ~66.4
Production Reality: Always call set.seed() before generating random numbers if you need reproducible results — critical for sharing analysis or debugging.
6. Other Common Distributions
| Distribution | Use Case | R Functions |
|---|---|---|
| Binomial | Number of successes in n trials | dbinom, pbinom, rbinom |
| Poisson | Count of rare events over time/space | dpois, ppois, rpois |
| Uniform | Equal probability across a range | dunif, punif, runif |
| Exponential | Time between events | dexp, pexp, rexp |
# Probability of exactly 3 successes in 10 trials, p = 0.5
dbinom(3, size = 10, prob = 0.5)
# Simulate 5 coin-flip experiments
rbinom(5, size = 10, prob = 0.5)
7. Correlation Between Variables
cor(df$age, df$salary) # Pearson correlation coefficient
cor(df[, c("age", "salary", "experience")]) # correlation matrix
8. Production-Ready Checklist
- ✅ Master mean, median, sd, and quantile — the core summary statistics.
- ✅ Use summary() as your first exploratory step — on any new dataset.
- ✅ Know the d/p/q/r pattern — for working with any probability distribution.
- ✅ Always set.seed() — before random sampling, for reproducibility.
Pro Tip: Descriptive statistics aren't a formality before 'real' modeling — they're often where you catch data quality issues and form your first real hypotheses.
Ready to master R Programming?
Build real-world data analysis and visualization projects with hands-on training, mentor-led sessions, and placement support.
.png)