R Cheatsheet

Complete R reference. Hit Ctrl+P to print.

Basics

x <- 42Assign value (preferred assignment operator)
x = 42Assignment (also works; <- preferred by convention)
x <<- 42Global assignment from within a function
# commentComment
print(x)Print value
cat("Hello", name, "\n")Concatenate and print - no newline by default
class(x)Type/class of object
typeof(x)Internal storage type
is.numeric(x) / is.character(x) / is.logical(x)Type checks
as.numeric(x) / as.character(x) / as.integer(x)Type coercion
ls()List variables in environment
rm(x)Remove variable
rm(list = ls())Remove all variables
?mean / help(mean)Get help for function
getwd() / setwd("/path")Get/set working directory

Data Types

42LInteger literal (L suffix)
3.14Numeric (double)
"hello"Character (string)
TRUE / FALSE / T / FLogical values
1+2iComplex number
NAMissing value - any type
NA_integer_ / NA_real_ / NA_character_Typed NA values
NULLAbsence of value (different from NA)
NaNNot a Number (0/0)
Inf / -InfPositive/negative infinity
is.na(x) / is.null(x) / is.nan(x)Test for special values
factor(c("a","b","a"))Factor - categorical variable
factor(x, levels = c("low","mid","high"), ordered = TRUE)Ordered factor
as.Date("2024-01-15")Date object
Sys.time()Current datetime (POSIXct)

Vectors

c(1, 2, 3)Create vector - all elements same type
1:10Integer sequence 1 to 10
seq(0, 1, by = 0.1)Sequence with step
seq(0, 1, length.out = 11)Sequence of given length
rep(0, times = 5)Repeat value 5 times
rep(c(1, 2), each = 3)Repeat each element 3 times
v[3]Access element at index 3 (1-based!)
v[c(1, 3, 5)]Select elements by index vector
v[-1]Exclude element at index 1
v[v > 2]Logical subsetting - elements matching condition
which(v > 2)Indices of elements meeting condition
length(v)Number of elements
c(v, 99)Append to vector
v * 2Vectorized operation - applied to all elements
v + wElement-wise addition (recycling if lengths differ)
sum(v) / prod(v) / cumsum(v)Sum, product, cumulative sum
mean(v) / median(v) / var(v) / sd(v)Descriptive statistics
min(v) / max(v) / range(v)Min, max, range
sort(v) / order(v)Sorted values / indices that would sort
rev(v)Reverse vector
unique(v)Unique values
table(v)Frequency table
names(v) <- c("a", "b")Name vector elements
v["a"]Access by name

Matrices & Arrays

matrix(1:12, nrow = 3, ncol = 4)Create matrix - filled column-wise by default
matrix(1:12, nrow = 3, byrow = TRUE)Fill row-wise
m[2, 3]Element at row 2, column 3
m[1, ]First row
m[, 2]Second column
dim(m)Dimensions: c(rows, cols)
nrow(m) / ncol(m)Number of rows / columns
t(m)Transpose matrix
m %*% nMatrix multiplication
rbind(a, b)Stack matrices by rows
cbind(a, b)Stack matrices by columns
apply(m, 1, sum)Apply function to rows (1) or cols (2)
rowSums(m) / colSums(m) / rowMeans(m) / colMeans(m)Row/column aggregates

Lists

list(name = "Alice", age = 30, scores = c(90, 85))Create named list - heterogeneous
lst[[1]]Access first element
lst[["name"]]Access by name
lst$nameAccess named element with $
lst[1]Subset list - returns a list
length(lst)Number of elements
names(lst)Get element names
lst$new <- "value"Add element
lst$name <- NULLRemove element
lapply(lst, fn)Apply function - returns list
sapply(lst, fn)Apply function - simplify result to vector/matrix
rapply(lst, fn, how = "unlist")Recursively apply to list
do.call(rbind, lst)Call function with list as arguments

Data Frames

data.frame(name = c("Alice","Bob"), age = c(30, 25))Create data frame
df[1, ]First row
df[, "name"]Column by name
df$nameColumn with $ - returns vector
df[df$age > 25, ]Filter rows by condition
subset(df, age > 25, select = c(name, age))Subset with column selection
nrow(df) / ncol(df) / dim(df)Dimensions
head(df, 10) / tail(df, 10)First/last N rows
str(df)Structure - column types and preview
summary(df)Descriptive statistics per column
df$newcol <- df$a + df$bAdd computed column
df[, c("a", "b")]Select columns
df[order(df$age), ]Sort by column
merge(df1, df2, by = "id")Inner join on key column
merge(df1, df2, by = "id", all.x = TRUE)Left join
rbind(df1, df2)Stack data frames
cbind(df1, df2)Bind columns

Control Flow & Functions

if (x > 0) { } else if (x < 0) { } else { }if / else if / else
ifelse(v > 0, "pos", "neg")Vectorized if-else
for (i in 1:10) { }for loop over sequence
for (item in lst) { }for loop over list or vector
while (x > 0) { x <- x - 1 }while loop
repeat { if (done) break }repeat loop - must break manually
break / nextExit loop / skip to next iteration
add <- function(a, b = 0) { a + b }Function definition with default
add(a = 1, b = 2)Named argument call
f <- function(...) { args <- list(...) }Variadic function - ... captures extra args
do.call(f, list(1, 2, 3))Call function with argument list
(function(x) x + 1)(5)Immediately-invoked function
|> or %>%Native pipe (R 4.1+) and magrittr pipe
x |> sqrt() |> round(2)Chain with native pipe
Reduce("+", 1:5)Reduce vector with function
Filter(function(x) x > 2, v)Filter vector with function
Map(f, v1, v2)Apply f to paired elements - returns list

String Manipulation

nchar(s)Number of characters
paste("a", "b", sep = "-")Concatenate with separator
paste0("a", "b")Concatenate without separator
sprintf("%.2f%%", 3.14159)Formatted string (printf-style)
toupper(s) / tolower(s)Change case
trimws(s)Trim whitespace from both ends
substr(s, 1, 3)Substring (start, stop - 1-based, inclusive)
strsplit("a,b,c", ",")Split string - returns list
grepl("pattern", s)Returns TRUE if pattern found
grep("pattern", v)Indices of matching elements
regmatches(s, regexpr("\d+", s))Extract first regex match
sub("old", "new", s)Replace first match
gsub("old", "new", s)Replace all matches
startsWith(s, "Hi") / endsWith(s, "!")Prefix/suffix check
formatC(3.14, format = "f", digits = 2)Format number as string

I/O & Packages

read.csv("file.csv", header = TRUE)Read CSV file
read.table("file.txt", sep = "\t", header = TRUE)Read delimited text file
readLines("file.txt")Read text file as character vector
write.csv(df, "out.csv", row.names = FALSE)Write CSV file
writeLines(v, "out.txt")Write character vector to file
saveRDS(obj, "file.rds")Save single R object
obj <- readRDS("file.rds")Load single R object
save(a, b, file = "data.RData")Save multiple objects
load("data.RData")Load saved objects into environment
install.packages("dplyr")Install package from CRAN
library(dplyr)Load package
require(dplyr)Load package - returns FALSE instead of error
installed.packages()List installed packages
update.packages()Update all packages
dplyr::filter(df, age > 25)Qualify function with package name