Step 1 of 4 · Reading · ~1 min
Learn
Objects and Stats
R was made for stats. Most descriptive stats are one-liners.
One thing to fix in your head before the numbers surprise you: var and sd are the sample versions, dividing the squared deviations by n - 1, not by n. Hand-computed textbook answers that divide by n come out slightly smaller and will not match R.
x <- c(2, 4, 4, 4, 5, 5, 7, 9)
mean(x) # 5
median(x) # 4.5
var(x) # 4.571429 -- SAMPLE variance, divided by n - 1
sd(x) # 2.13809 -- sqrt of that
range(x) # c(2, 9)
quantile(x) # 0% 2.0 25% 4.0 50% 4.5 75% 5.5 100% 9.0
IQR(x) # 1.5 (interquartile range: 5.5 - 4.0)
summary() — quick descriptive overview, especially useful on data frames:
summary(c(1, 2, 3, 4, 5))
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 1 2 3 3 4 5
cor for correlation, cov for covariance:
x <- 1:10
y <- x + rnorm(10, sd=2)
cor(x, y) # ~0.9 — strong positive correlation
Linear regression with lm:
model <- lm(y ~ x)
summary(model) # coefficients, R², p-values
coef(model) # intercept and slope
predict(model, newdata=data.frame(x=20))
This is just the start — R has 18,000+ packages on CRAN for every statistical method imaginable. Tools like tidyverse (dplyr, ggplot2, tidyr) modernize the workflow but build on these primitives.
hist() and plot() — R's plotting is one-line, ASCII fallback when no display.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…