The statistics-first language โ built specifically for analyzing data and making plots, and still the default in most college statistics departments (Python has taken over general data science, but R remains huge in stats, biostatistics, and research).
R's core data structure โ everything is really built on vectors (even a single number is a vector of length 1).
nums <- c(4, 8, 15, 16, 23, 42)
nums[1] # 4 โ R is 1-indexed, not 0-indexed!
sum(nums) # 108
nums * 2 # multiplies EVERY element โ no loop needed ("vectorized")
R's table type โ like a spreadsheet, and the object most real R code revolves around.
df <- data.frame(
name = c("Ada", "Linus", "Grace"),
grade = c(92, 88, 95)
)
df$grade # the "grade" column, as a vector
nrow(df) # 3
df[df$grade >= 90, ] # base R โ rows where grade >= 90
# the tidyverse way (very common in practice):
library(dplyr)
df |> filter(grade >= 90)
mean(nums) # average
median(nums)
sd(nums) # standard deviation
summary(nums) # min, quartiles, mean, max all at once
cor(x, y) # correlation between two vectors
plot(nums) # quick base-R scatter plot
# ggplot2 โ the standard for real/publication-quality plots:
library(ggplot2)
ggplot(df, aes(x = name, y = grade)) + geom_col()
add <- function(a, b) {
return(a + b)
}
add(2, 3) # 5