R Cheat Sheet

Free R cheat sheet: the most-used R syntax and methods at a glance — searchable and beginner-friendly.

Basics & Vectors

ConceptSyntaxExample
Assign a value
<- is idiomatic R; = also works for assignment.
x <- 5x <- 5 y = 10
Create a vector
c() combines values into an atomic vector.
c(1, 2, 3)nums <- c(1, 2, 3, 4)
Sequence
1:n is a quick integer sequence; seq() gives full control.
seq(from, to, by) / 1:nseq(0, 10, by = 2) 1:5
Vectorized math
Operations apply element-wise across the whole vector.
vec * 2 + 1c(1, 2, 3) * 10 # 10 20 30
Index a vector
R indexing starts at 1; negative indices drop elements.
vec[i]nums[1] # first element (1-based) nums[-1] # all but first

Data Frames

ConceptSyntaxExample
Create a data frame
Columns are equal-length vectors.
data.frame(col = ...)df <- data.frame(name = c("Ada", "Bob"), age = c(36, 28))
Inspect
str() shows structure; summary() gives per-column stats.
head(df) / str(df) / summary(df)head(df) str(df)
Select a column
$ returns a vector for that column.
df$col / df[["col"]]df$age
Subset rows/cols
Leave a slot blank to keep all rows or columns.
df[rows, cols]df[df$age > 30, c("name", "age")]
Dimensions
dim() returns c(rows, cols).
nrow(df) / ncol(df) / dim(df)nrow(df) # number of rows

dplyr

ConceptSyntaxExample
Load dplyr
Part of the tidyverse; provides verbs for data wrangling.
library(dplyr)library(dplyr)
Pipe
%>% passes the left side as the first argument on the right.
df %>% verb()df %>% filter(age > 30)
Filter & select
filter keeps rows; select keeps columns.
filter(cond) / select(cols)df %>% filter(age > 30) %>% select(name)
Mutate & arrange
mutate adds columns; arrange sorts (desc() for descending).
mutate(new = ...) / arrange(col)df %>% mutate(adult = age >= 18) %>% arrange(desc(age))
Group & summarise
Split-apply-combine for grouped aggregates.
group_by(col) %>% summarise(...)df %>% group_by(city) %>% summarise(mean_age = mean(age))

ggplot2

ConceptSyntaxExample
Load ggplot2
The grammar-of-graphics plotting package.
library(ggplot2)library(ggplot2)
Base plot + aesthetics
aes() maps data columns to visual properties.
ggplot(df, aes(x, y))ggplot(df, aes(x = age, y = salary))
Scatter / point layer
Add geoms with + to draw the data.
+ geom_point()ggplot(df, aes(age, salary)) + geom_point()
Line & bar
geom_bar(stat = "identity") uses y values directly.
+ geom_line() / + geom_bar()ggplot(df, aes(x = month, y = sales)) + geom_line()
Labels & themes
labs() sets titles; theme_*() restyles the whole plot.
+ labs(...) + theme_minimal()p + labs(title = "Sales", x = "Month") + theme_minimal()

Stats & Models

ConceptSyntaxExample
Summary statistics
Add na.rm = TRUE to ignore missing values.
mean(x) / median(x) / sd(x)mean(c(1, 2, 3, 4)) # 2.5
Correlation
Pearson correlation by default, from -1 to 1.
cor(x, y)cor(df$age, df$salary)
Linear model
Fit ordinary least squares regression.
lm(y ~ x, data = df)model <- lm(salary ~ age, data = df)
Model summary
Coefficients, p-values, R-squared and residuals.
summary(model)summary(model)
t-test
Compare means of two samples.
t.test(x, y)t.test(group1, group2)

I/O

ConceptSyntaxExample
Read CSV
readr::read_csv() is a faster tidyverse alternative.
read.csv("file.csv")df <- read.csv("data.csv")
Write CSV
row.names = FALSE drops the index column.
write.csv(df, "out.csv")write.csv(df, "out.csv", row.names = FALSE)
Install / load package
Install once, load every session.
install.packages() / library()install.packages("dplyr") library(dplyr)
Print to console
cat() concatenates without quotes; print() shows structure.
print(x) / cat(...)cat("Total:", sum(nums), "\n")
Get help
Opens the documentation for any function.
?function / help()?mean