R: Cheat sheet for everyday computational analysis
cheatsheet
R
Quick reference cheatsheet for frequently used commands for R
R quick reference
R basics
| Function/operator | Purpose | Example |
|---|---|---|
c() |
Combine values into a vector | c(1, 2, 3) |
<- |
Assignment | x <- 5 |
read.delim() |
Read a tab-delimited file | read.delim("data/raw/file.txt") |
str() |
Show structure/types of an object | str(df) |
dim(), nrow(), ncol() |
Dimensions of a data frame | dim(df) |
head() / tail() |
First/last rows | head(df, 5) |
summary() |
Quick numeric/factor summary | summary(df$Height) |
df$col |
Access a column by name | df$Fitness |
df[rows, cols] |
Subset by position/condition | df[df$Region == "E", ] |
table() |
Count occurrences | table(df$Population) |
tapply() / aggregate() |
Grouped summaries | tapply(df$Height, df$Population, mean) |
function(...) {} |
Define a function | f <- function(x) mean(x, na.rm = TRUE) |
for (i in seq) |
Loop | for (p in unique(df$Population)) {...} |
hist(), boxplot(), plot() |
Base R plotting | hist(df$Height) |
write.csv() |
Save a table | write.csv(x, "results/out.csv") |
R for bioinformatics quick reference
| Function/package | Purpose | Example |
|---|---|---|
matrix() |
Build a genotype matrix | matrix(..., nrow, ncol) |
colMeans() |
Column-wise means (used for allele freq.) | colMeans(geno) / 2 |
prcomp() |
Principal component analysis | prcomp(geno, scale. = TRUE) |
cor.test() |
Test correlation between two variables | cor.test(x, y) |
lm() |
Fit a linear model | lm(y ~ x1 + x2, data = df) |
vegan::rda() |
Redundancy analysis (multivariate GEA) | rda(Y ~ x1 + x2, data = df) |
vcfR::read.vcfR() |
Read a real VCF file (beyond this module) | read.vcfR("file.vcf") |
adegenet::genind |
Genotype container object (beyond this module) | df2genind(...) |