R Cheat Sheet
R syntax, vectors, data frames, and core statistical functions for data analysis, wrangling, and visualization.
2 PagesBeginnerApr 12, 2026
Basic Syntax
Assignment, control flow, and printing.
r
age <- 30 # assignment operatorname <- "Ada"pi_val <- 3.14159is_fun <- TRUEif (age >= 18) { print(paste(name, "is an adult"))}for (i in 1:5) { print(paste("Count:", i))}
Vectors & Data Frames
R's core data structures.
r
nums <- c(5, 3, 1, 4, 2) # numeric vectorsorted <- sort(nums) # 1 2 3 4 5nums[2] # 3 (1-indexed)df <- data.frame( name = c("Alice", "Bob"), age = c(30, 25))df$age # 30 25df[df$age > 26, ] # rows where age > 26
Statistical Functions
Common base-R statistics functions.
- mean(x)- arithmetic average of a numeric vector
- median(x)- middle value of a sorted vector
- sd(x)- sample standard deviation
- summary(x)- min, quartiles, mean, and max of a vector/data frame
- lm(y ~ x)- fits a linear regression model
- table(x)- frequency counts of categorical values
- is.na(x)- returns TRUE for missing (NA) values
The Apply Family
Vectorized iteration over lists and matrices.
r
nums <- list(1:3, 4:6, 7:9)sapply(nums, sum) # 6 15 24 (simplified vector)lapply(nums, mean) # list of meansvapply(nums, sum, numeric(1)) # 6 15 24 (type-checked)m <- matrix(1:6, nrow = 2)apply(m, 1, sum) # row sumsapply(m, 2, sum) # column sums
dplyr Data Wrangling
Pipe-based data manipulation with the tidyverse.
r
library(dplyr)result <- mtcars %>% filter(mpg > 20) %>% group_by(cyl) %>% summarise( avg_hp = mean(hp), n = n() ) %>% arrange(desc(avg_hp))# Add / modify columnsmtcars %>% mutate(kpl = mpg * 0.425)# Native pipe (R >= 4.1)mtcars |> subset(mpg > 20)
Plotting with ggplot2
Layered grammar-of-graphics visualization.
r
library(ggplot2)ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) + geom_point(size = 3) + geom_smooth(method = "lm", se = FALSE) + labs(title = "MPG vs Weight", x = "Weight", y = "MPG") + theme_minimal()# Save the last plotggsave("plot.png", width = 6, height = 4, dpi = 300)
Writing Functions
Defining functions with defaults and variadic args.
r
normalize <- function(x, na.rm = TRUE) { (x - min(x, na.rm = na.rm)) / (max(x, na.rm = na.rm) - min(x, na.rm = na.rm))}normalize(c(1, 5, 10)) # 0.0 0.444 1.0# Variadic arguments via ...my_sum <- function(...) { args <- c(...) sum(args)}my_sum(1, 2, 3, 4) # 10
Reading & Writing Data
Common import/export functions.
- read.csv(f)- read a CSV into a data frame (base R)
- readr::read_csv(f)- faster tidyverse CSV reader returning a tibble
- readRDS(f)- load a single serialized R object
- saveRDS(x, f)- serialize one R object to disk
- read_excel(f)- read .xlsx sheets via the readxl package
- write.csv(x, f)- export a data frame to CSV
- load()/save()- restore or store multiple named objects in .RData
Pro Tip
Use vectorized operations (nums * 2) instead of for-loops in R — the interpreter is optimized for vector math and loops are comparatively slow.
Was this cheat sheet helpful?
Explore Topics
#RCheatSheet#Programming#Beginner#BasicSyntax#VectorsDataFrames#StatisticalFunctions#TheApplyFamily#Functions#CheatSheet#SkillVeris