๐Ÿ“ˆ

R Cheat Sheet

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).

๐Ÿ“ Reference page for now โ€” R doesn't yet have an in-browser runtime as mature as Python's Pyodide, so there's no live "Run" button here. The free Posit Cloud or a local RStudio install are the standard ways to actually run these examples today.
Jump to: VectorsData framesFiltering Statistics functionsPlottingFunctions

Vectors

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")

Data frames

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

Filtering

df[df$grade >= 90, ]     # base R โ€” rows where grade >= 90

# the tidyverse way (very common in practice):
library(dplyr)
df |> filter(grade >= 90)

Statistics functions

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

Plotting

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()

Functions

add <- function(a, b) {
  return(a + b)
}
add(2, 3)   # 5
โ† See the Data Scientist career track