R Descriptive Statistics
Descriptive statistics summarize and describe the main features of a dataset using numbers. They answer basic questions about your data before any modeling: What is the typical value? How spread out are the values? What is the shape of the distribution? R calculates all standard descriptive statistics in one or two lines.
Measures of Central Tendency
scores <- c(55, 70, 72, 75, 78, 80, 82, 85, 88, 90, 92, 95, 45, 100, 68) mean(scores) # 78.33 — arithmetic average median(scores) # 80 — middle value when sorted
# Mode — most frequent value (no built-in function, write your own)
mode_val <- function(x) {
tab <- table(x)
as.numeric(names(tab)[tab == max(tab)])
}
Central Tendency Diagram:
Sorted: 45 55 68 70 72 75 78 80 82 85 88 90 92 95 100
↑
Median = 80
Mean = (sum of all) / 15 = 78.33
Median vs Mean:
Symmetric data: mean ≈ median
Right-skewed: mean > median (e.g. incomes)
Left-skewed: mean < median
Measures of Spread
var(scores) # 218.52 — variance (average squared deviation) sd(scores) # 14.78 — standard deviation (in original units) range(scores) # 45 100 — min and max diff(range(scores)) # 55 — range width IQR(scores) # 20 — interquartile range (Q3 - Q1) mad(scores) # median absolute deviation
Quantiles and Percentiles
quantile(scores) # 0%, 25%, 50%, 75%, 100% quantile(scores, 0.25) # Q1 = 70 quantile(scores, 0.75) # Q3 = 90 quantile(scores, c(0.1, 0.9)) # 10th and 90th percentile # Custom percentiles quantile(scores, probs=seq(0, 1, 0.2))
Box-and-whisker representation of quantiles:
Q0 Q1 Q2 Q3 Q4
|────|───────|─────|────|
45 70 80 90 100
└──────────────┘
IQR = 20
summary() — One-Call Overview
summary(scores) # Min. 1st Qu. Median Mean 3rd Qu. Max. # 45.0 70.0 80.0 78.3 90.0 100.0
Skewness and Kurtosis
library(e1071) skewness(scores) # < 0: left-skewed, > 0: right-skewed, ≈ 0: symmetric kurtosis(scores) # measures tail heaviness
Skewness guide: -0.5 to 0.5 → roughly symmetric 0.5 to 1.0 → moderately right-skewed > 1.0 → highly right-skewed
Descriptive Stats for a Data Frame
students <- data.frame( age = c(20,22,21,25,23,24,22,21,26,20), score = c(78,85,72,90,68,88,75,80,92,65), salary = c(25000,35000,28000,45000,30000,40000,32000,38000,50000,27000) ) summary(students) # age score salary # Min. :20.0 Min. :65.0 Min. :25000 # 1st Qu.:21.0 1st Qu.:74.2 1st Qu.:28500 # Median :22.0 Median :79.5 Median :33500 # Mean :22.4 Mean :79.3 Mean :35000 # 3rd Qu.:23.8 3rd Qu.:86.8 3rd Qu.:39500 # Max. :26.0 Max. :92.0 Max. :50000 # All stats at once per column sapply(students, function(x) c( mean=mean(x), sd=round(sd(x),2), min=min(x), max=max(x), median=median(x) ))
Frequency Tables
grades <- c("A","B","A","C","B","A","D","B","A","C")
table(grades)
#grades
# A B C D
# 4 3 2 1
prop.table(table(grades)) # proportions
#grades
# A B C D
# 0.4 0.3 0.2 0.1
Correlation Overview
cor(students$age, students$score) # single pair cor(students) # full correlation matrix
Descriptive statistics are always the first step in any analysis. Before fitting a model or creating a visualization, run summary() on your data frame and check distributions, ranges, and missing value counts. These numbers tell you what you are working with and flag any issues before they cause problems downstream.
