Skip to content
Basic Statistics
step 1/4

Reading — step 1 of 4

Learn

~1 min readObjects and Stats

R was made for stats. Most descriptive stats are one-liners.

x <- c(2, 4, 4, 4, 5, 5, 7, 9)

mean(x)            # 5
median(x)          # 4.5
var(x)             # 4 (sample variance)
sd(x)              # 2 (sample std dev)
range(x)           # c(2, 9)
quantile(x)        # 0%, 25%, 50%, 75%, 100%
IQR(x)             # 3 (interquartile range)

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…