R Programming Cheat Sheet

Master R programming with this comprehensive reference guide. Find functions, data manipulation techniques, statistical methods, and examples for effective data analysis.

46 functions found
Filter by category:

c()

Data Structures

Create a vector

Syntax:

c(values)

Examples:

c(1, 2, 3, 4, 5) Create numeric vector
c('a', 'b', 'c') Create character vector
c(TRUE, FALSE, TRUE) Create logical vector

Notes:

Fundamental data structure in R

list()

Data Structures

Create a list

Syntax:

list(elements)

Examples:

list(a = 1, b = 2, c = 3) Named list
list(c(1,2), c('a','b')) List of vectors

Notes:

Can contain different data types

data.frame()

Data Structures

Create a data frame

Syntax:

data.frame(col1, col2, ...)

Examples:

data.frame(x = 1:5, y = letters[1:5]) Create data frame with vectors
data.frame(name = c('Alice', 'Bob'), age = c(25, 30)) Create data frame with named columns

Notes:

Most common data structure for datasets

matrix()

Data Structures

Create a matrix

Syntax:

matrix(data, nrow, ncol)

Examples:

matrix(1:12, nrow = 3, ncol = 4) 3x4 matrix
matrix(0, nrow = 2, ncol = 3) 2x3 zero matrix

Notes:

Two-dimensional array of same data type

factor()

Data Structures

Create categorical variable

Syntax:

factor(x, levels, labels)

Examples:

factor(c('low', 'high', 'medium')) Create factor
factor(c(1,2,1,3), labels = c('A','B','C')) Factor with custom labels

Notes:

Used for categorical data

subset()

Data Manipulation

Filter data based on conditions

Syntax:

subset(data, condition)

Examples:

subset(mtcars, mpg > 20) Filter rows where mpg > 20
subset(mtcars, cyl == 4, select = c(mpg, hp)) Filter and select columns

Notes:

Convenient for data filtering

merge()

Data Manipulation

Join data frames

Syntax:

merge(x, y, by)

Examples:

merge(df1, df2, by = 'id') Inner join by id column
merge(df1, df2, by = 'id', all = TRUE) Full outer join

Notes:

SQL-like joins for data frames

aggregate()

Data Manipulation

Group and summarize data

Syntax:

aggregate(x, by, FUN)

Examples:

aggregate(mpg ~ cyl, data = mtcars, mean) Mean mpg by cylinder
aggregate(. ~ Species, data = iris, mean) Mean of all variables by Species

Notes:

Group by operations

apply()

Data Manipulation

Apply function over margins

Syntax:

apply(X, MARGIN, FUN)

Examples:

apply(mtcars, 2, mean) Column means
apply(mtcars, 1, sum) Row sums

Notes:

MARGIN: 1=rows, 2=columns

lapply()

Data Manipulation

Apply function to list elements

Syntax:

lapply(X, FUN)

Examples:

lapply(mtcars, mean) Mean of each column
lapply(1:3, function(x) x^2) Square each element

Notes:

Returns a list

sapply()

Data Manipulation

Simplified lapply

Syntax:

sapply(X, FUN)

Examples:

sapply(mtcars, mean) Mean of each column as vector
sapply(iris[1:4], function(x) c(min(x), max(x))) Min and max for each column

Notes:

Returns vector/matrix when possible

mean()

Statistics

Calculate arithmetic mean

Syntax:

mean(x, na.rm = FALSE)

Examples:

mean(c(1, 2, 3, 4, 5)) Mean of vector
mean(mtcars$mpg) Mean of mpg column
mean(c(1, 2, NA, 4), na.rm = TRUE) Mean excluding NA values

Notes:

Use na.rm = TRUE to handle missing values

median()

Statistics

Calculate median

Syntax:

median(x, na.rm = FALSE)

Examples:

median(c(1, 2, 3, 4, 5)) Median of vector
median(mtcars$mpg) Median of mpg column

Notes:

Middle value when data is sorted

sd()

Statistics

Calculate standard deviation

Syntax:

sd(x, na.rm = FALSE)

Examples:

sd(mtcars$mpg) Standard deviation of mpg
sd(rnorm(100)) SD of random normal data

Notes:

Sample standard deviation

var()

Statistics

Calculate variance

Syntax:

var(x, na.rm = FALSE)

Examples:

var(mtcars$mpg) Variance of mpg
var(iris$Sepal.Length) Variance of sepal length

Notes:

Sample variance

cor()

Statistics

Calculate correlation

Syntax:

cor(x, y = NULL, method)

Examples:

cor(mtcars$mpg, mtcars$hp) Correlation between mpg and hp
cor(mtcars) Correlation matrix
cor(x, y, method = 'spearman') Spearman correlation

Notes:

Default is Pearson correlation

t.test()

Statistics

Perform t-test

Syntax:

t.test(x, y = NULL, alternative)

Examples:

t.test(rnorm(30), mu = 0) One-sample t-test
t.test(group1, group2) Two-sample t-test
t.test(x ~ group, data = df) t-test with formula

Notes:

Tests for differences in means

plot()

Plotting

Create scatter plot

Syntax:

plot(x, y, type, main, xlab, ylab)

Examples:

plot(mtcars$hp, mtcars$mpg) Scatter plot
plot(1:10, type = 'l') Line plot
plot(x, y, main = 'Title', xlab = 'X', ylab = 'Y') Plot with labels

Notes:

Base plotting function

hist()

Plotting

Create histogram

Syntax:

hist(x, breaks, main, xlab)

Examples:

hist(mtcars$mpg) Histogram of mpg
hist(rnorm(1000), breaks = 30) Histogram with 30 bins

Notes:

Shows distribution of continuous data

boxplot()

Plotting

Create box plot

Syntax:

boxplot(x, main, xlab, ylab)

Examples:

boxplot(mtcars$mpg) Box plot of mpg
boxplot(mpg ~ cyl, data = mtcars) Box plot by group

Notes:

Shows distribution and outliers

barplot()

Plotting

Create bar plot

Syntax:

barplot(height, names.arg, main)

Examples:

barplot(table(mtcars$cyl)) Bar plot of counts
barplot(c(3, 7, 1), names.arg = c('A', 'B', 'C')) Bar plot with names

Notes:

Good for categorical data

read.csv()

Data Import/Export

Read CSV file

Syntax:

read.csv(file, header = TRUE, sep = ',')

Examples:

read.csv('data.csv') Read CSV file
read.csv('data.csv', header = FALSE) Read CSV without header
read.csv('data.txt', sep = '\t') Read tab-separated file

Notes:

Most common way to read data

write.csv()

Data Import/Export

Write CSV file

Syntax:

write.csv(x, file, row.names = TRUE)

Examples:

write.csv(mtcars, 'cars.csv') Write data frame to CSV
write.csv(df, 'data.csv', row.names = FALSE) Write without row names

Notes:

Export data to CSV format

read.table()

Data Import/Export

Read delimited file

Syntax:

read.table(file, header, sep)

Examples:

read.table('data.txt', header = TRUE) Read table with header
read.table('data.dat', sep = ' ') Read space-separated file

Notes:

More flexible than read.csv

if/else

Programming

Conditional statements

Syntax:

if (condition) { ... } else { ... }

Examples:

if (x > 0) print('positive') Simple if statement
if (x > 0) { print('pos') } else { print('neg') } If-else statement
ifelse(x > 0, 'pos', 'neg') Vectorized conditional

Notes:

Use ifelse() for vectorized operations

for

Programming

For loop

Syntax:

for (var in sequence) { ... }

Examples:

for (i in 1:10) print(i) Loop through numbers
for (name in names(mtcars)) print(name) Loop through column names

Notes:

Iterate over sequences

while

Programming

While loop

Syntax:

while (condition) { ... }

Examples:

i <- 1; while (i <= 5) { print(i); i <- i + 1 } While loop example

Notes:

Loop while condition is true

function()

Programming

Define function

Syntax:

function(args) { body }

Examples:

square <- function(x) x^2 Simple function
greet <- function(name = 'World') paste('Hello', name) Function with default argument

Notes:

Functions are first-class objects

paste()

String Operations

Concatenate strings

Syntax:

paste(..., sep = ' ', collapse = NULL)

Examples:

paste('Hello', 'World') Concatenate with space
paste0('Hello', 'World') Concatenate without separator
paste(c('a', 'b'), 1:2, sep = '-') Vectorized concatenation

Notes:

paste0() is equivalent to paste(..., sep = '')

substr()

String Operations

Extract substring

Syntax:

substr(x, start, stop)

Examples:

substr('Hello World', 1, 5) Extract 'Hello'
substr(c('abc', 'def'), 1, 2) Extract first 2 characters

Notes:

Positions are 1-indexed

nchar()

String Operations

Number of characters

Syntax:

nchar(x)

Examples:

nchar('Hello') Returns 5
nchar(c('a', 'abc', 'hello')) Vector of lengths

Notes:

Counts characters in each element

grep()

String Operations

Pattern matching

Syntax:

grep(pattern, x, value = FALSE)

Examples:

grep('mt', rownames(mtcars)) Find rows containing 'mt'
grep('mt', rownames(mtcars), value = TRUE) Return matching values
grepl('mt', rownames(mtcars)) Return logical vector

Notes:

Use grepl() for logical output

is.na()

Missing Values

Test for missing values

Syntax:

is.na(x)

Examples:

is.na(c(1, 2, NA, 4)) Returns logical vector
sum(is.na(df)) Count total missing values
which(is.na(x)) Positions of missing values

Notes:

Returns TRUE for NA values

na.omit()

Missing Values

Remove missing values

Syntax:

na.omit(object)

Examples:

na.omit(c(1, 2, NA, 4)) Remove NAs from vector
na.omit(df) Remove rows with any NA

Notes:

Removes complete cases only

complete.cases()

Missing Values

Find complete cases

Syntax:

complete.cases(...)

Examples:

complete.cases(df) Logical vector of complete rows
df[complete.cases(df), ] Keep only complete cases

Notes:

Returns TRUE for rows without NA

length()

Utilities

Length of object

Syntax:

length(x)

Examples:

length(c(1, 2, 3, 4, 5)) Returns 5
length(mtcars) Number of columns

Notes:

For data frames, returns number of columns

dim()

Utilities

Dimensions of object

Syntax:

dim(x)

Examples:

dim(mtcars) Returns c(32, 11)
dim(matrix(1:12, 3, 4)) Returns c(3, 4)

Notes:

Returns NULL for vectors

str()

Utilities

Structure of object

Syntax:

str(object)

Examples:

str(mtcars) Display structure of data frame
str(list(a = 1, b = 'hello')) Structure of list

Notes:

Compact display of object structure

summary()

Utilities

Summary statistics

Syntax:

summary(object)

Examples:

summary(mtcars) Summary of all columns
summary(mtcars$mpg) Summary of single variable

Notes:

Provides different summaries for different object types

head()

Utilities

First few elements

Syntax:

head(x, n = 6)

Examples:

head(mtcars) First 6 rows
head(mtcars, 10) First 10 rows

Notes:

Quick peek at data

tail()

Utilities

Last few elements

Syntax:

tail(x, n = 6)

Examples:

tail(mtcars) Last 6 rows
tail(mtcars, 3) Last 3 rows

Notes:

View end of data

names()

Utilities

Names of object elements

Syntax:

names(x)

Examples:

names(mtcars) Column names
names(list(a = 1, b = 2)) List element names

Notes:

Get or set names

rnorm()

Random Numbers

Random normal numbers

Syntax:

rnorm(n, mean = 0, sd = 1)

Examples:

rnorm(10) 10 standard normal numbers
rnorm(100, mean = 5, sd = 2) 100 numbers with mean=5, sd=2

Notes:

Generates from normal distribution

runif()

Random Numbers

Random uniform numbers

Syntax:

runif(n, min = 0, max = 1)

Examples:

runif(10) 10 numbers between 0 and 1
runif(5, min = 1, max = 100) 5 numbers between 1 and 100

Notes:

Generates from uniform distribution

sample()

Random Numbers

Random sampling

Syntax:

sample(x, size, replace = FALSE)

Examples:

sample(1:10, 5) Sample 5 numbers from 1 to 10
sample(c('A', 'B', 'C'), 10, replace = TRUE) Sample with replacement

Notes:

Sample from existing vector

set.seed()

Random Numbers

Set random seed

Syntax:

set.seed(seed)

Examples:

set.seed(123); rnorm(5) Reproducible random numbers

Notes:

Ensures reproducible results

R Programming Mastery Guide

🌱 Beginner

  • • Learn basic data structures: vectors, lists, data.frames
  • • Master data import/export with read.csv() and write.csv()
  • • Practice basic statistics: mean(), median(), sd()
  • • Create simple plots with plot(), hist(), boxplot()
  • • Understand indexing with [] and $

📈 Intermediate

  • • Data manipulation with apply(), lapply(), sapply()
  • • Data filtering and subsetting with subset()
  • • Merge and join data frames with merge()
  • • Handle missing values with is.na(), na.omit()
  • • Write custom functions with function()

🚀 Advanced

  • • Advanced statistics: t.test(), cor(), regression
  • • Data aggregation with aggregate()
  • • String operations with grep(), paste(), substr()
  • • Control structures: if/else, for, while loops
  • • Random sampling and simulation with rnorm(), sample()

💡 Quick Tips

Data Analysis Workflow:

  1. Import data with read.csv()
  2. Explore with str(), summary(), head()
  3. Clean and manipulate data
  4. Analyze with statistical functions
  5. Visualize with plotting functions

Common Gotchas:

  • R is case-sensitive: Data ≠ data
  • Use na.rm = TRUE for functions with missing values
  • Vectors are 1-indexed, not 0-indexed