R Functions & Syntax Reference

76 directly linkable functions, operators and concepts used across the 38 ferdo Lab teaching tasks. Read the syntax, a plain-English description and precisely which exercise uses it.

About this source: This is an independently assembled, self-hosted ferdo Lab reference distilled from our original Masterclass teaching explanations, not a downloaded or verbatim copy of Metodi Siromahov's PALS0046 R Cheatsheet on RPubs. That RPubs document was not found in the checked private Moodle archive. This local reference is intended for stable internal deep linking.
76 / 76

Core R, syntax and objects (14)

[ ] · W1-M03
Select one or more values by 1-based position or logical mask.x[i]

In the course: W1-M03

<- · W1-M02
Assign the result of the right-hand expression to an object name.name <- expression

In the course: W1-M02

|> · W2-M07, W3-M05, W4-M04
Pass the preceding result to the next function call.data |> function()

In the course: W2-M07 · W3-M05 · W4-M04

all() · W3-M04
Return TRUE only if every tested value is TRUE. Check argument names and data types before running the code.all(logical)

In the course: W3-M04

exists() · W1-M06
Check whether an object of the specified name exists. Check argument names and data types before running the code.exists("x")

In the course: W1-M06

factor() · W2-M09, W4-M02, W4-M03, W4-M04
Represent categorical values with ordered levels and labels. Incorrect levels or labels can reverse category meanings.factor(x, levels=..., labels=...)

In the course: W2-M09 · W4-M02 · W4-M03 · W4-M04

identical() · W3-M05
Test strict equality, including data types and attributes. Check argument names and data types before running the code.identical(x, y)

In the course: W3-M05

ls() · W1-M02
List names of objects currently in the R environment. Check argument names and data types before running the code.ls()

In the course: W1-M02

paste0() · W3-M02, W3-M05
Join text and values without separators. Check argument names and data types before running the code.paste0("Q", 1:10)

In the course: W3-M02 · W3-M05

rm() · W1-M06
Remove an object from the current R environment. Check argument names and data types before running the code.rm(object)

In the course: W1-M06

seq_along() · W1-E01
Create integer indices from 1 through length(x). Check argument names and data types before running the code.seq_along(x)

In the course: W1-E01

stopifnot() · W2-M02, W3-M03, W3-M04, W3-M05 …
Stop execution with an error if a logical condition is not true. Check argument names and data types before running the code.stopifnot(condition)

In the course: W2-M02 · W3-M03 · W3-M04 · W3-M05 · W6-E02

dplyr, tidyr and data transformations (13)

%in% · W2-M04
Return TRUE when each element belongs to the specified set of values.x %in% c(0,1)

In the course: W2-M04

all_of() · W3-M02, W3-M05
Select columns identified by a character vector of names. Check argument names and data types before running the code.dplyr::all_of(names)

In the course: W3-M02 · W3-M05

anyNA() · W3-M03
Check whether an object contains missing values. Check argument names and data types before running the code.anyNA(x)

In the course: W3-M03

arrange() · W2-M05
Sort rows by one or more columns. Check argument names and data types before running the code.dplyr::arrange(data, col)

In the course: W2-M05

filter() · W2-M04, W2-E01, W3-M06
Keep rows satisfying the specified logical condition. Rows with an NA condition are not retained.dplyr::filter(data, condition)

In the course: W2-M04 · W2-E01 · W3-M06

group_by() · W2-M07, W3-M04, W3-M05, W4-M04
Set the grouping keys used by later operations. Remember to inspect or remove grouping after grouped summaries.dplyr::group_by(data, group)

In the course: W2-M07 · W3-M04 · W3-M05 · W4-M04

inner_join() · W2-M02, W2-E01, W3-M03, W3-M05 …
Keep and combine rows whose keys match in both tables. Check join keys, missing matches and possible duplicate-key expansion.dplyr::inner_join(x, y, by="id")

In the course: W2-M02 · W2-E01 · W3-M03 · W3-M05 · W4-E01

mutate() · W2-M08, W2-M09, W2-E01, W4-M02 …
Create or alter one or more columns. Check argument names and data types before running the code.dplyr::mutate(data, new=x+y)

In the course: W2-M08 · W2-M09 · W2-E01 · W4-M02 · W4-M04

n_distinct() · W2-M02
Count distinct values in a vector. Check argument names and data types before running the code.dplyr::n_distinct(x)

In the course: W2-M02

n() · W2-M06, W2-M07, W2-E01, W3-M06 …
Return the number of rows in the active dplyr group. Check argument names and data types before running the code.dplyr::n()

In the course: W2-M06 · W2-M07 · W2-E01 · W3-M06 · W4-M04

pivot_longer() · W3-M02, W3-M05
Reshape many columns into a smaller number of key/value columns. Check argument names and data types before running the code.tidyr::pivot_longer(data, cols=..., names_to=..., values_to=...)

In the course: W3-M02 · W3-M05

select() · W2-M03, W2-M05, W2-M08, W2-E01 …
Choose or rename columns in a data frame. Check argument names and data types before running the code.dplyr::select(data, col1, col2)

In the course: W2-M03 · W2-M05 · W2-M08 · W2-E01 · W3-M03

summarise() · W2-M06, W2-M07, W2-E01, W3-M04 …
Produce summary statistics for the whole table or each group. Check argument names and data types before running the code.dplyr::summarise(data, metric=mean(x))

In the course: W2-M06 · W2-M07 · W2-E01 · W3-M04 · W3-M05 · W3-M06 · W4-M04

ggplot2, visualisation and charts (17)

+ · W4-M01
Add a graphics layer; here + combines plot components, not numbers.ggplot(...) + geom_histogram(...)

In the course: W4-M01

aes() · W3-M07, W4-M01, W4-M02, W4-M03 …
Map data variables to graphical features such as x, y or colour. Check argument names and data types before running the code.ggplot2::aes(x=..., y=..., colour=...)

In the course: W3-M07 · W4-M01 · W4-M02 · W4-M03 · W4-M04 · W4-M05 · W4-M06

coord_cartesian() · W4-M05
Zoom a plot to a selected viewing range without discarding data. Check argument names and data types before running the code.ggplot2::coord_cartesian(ylim=c(lo, hi))

In the course: W4-M05

facet_wrap() · W4-M06
Split a plot into small panels by one or more grouping variables. Check argument names and data types before running the code.ggplot2::facet_wrap(~ group)

In the course: W4-M06

geom_boxplot() · W4-M03
Draw median, quartile and whisker summaries. The box is not a mean ± SD interval.ggplot2::geom_boxplot()

In the course: W4-M03

geom_col() · W4-M04
Draw bars using already-calculated y values. Check argument names and data types before running the code.ggplot2::geom_col()

In the course: W4-M04

geom_errorbar() · W4-M05
Draw uncertainty or variability intervals between ymin and ymax. Label whether intervals are SD, SE or confidence intervals.ggplot2::geom_errorbar(aes(ymin=..., ymax=...))

In the course: W4-M05

geom_histogram() · W3-M07, W4-M01
Draw bins showing a distribution of numeric values. Check argument names and data types before running the code.ggplot2::geom_histogram(binwidth=...)

In the course: W3-M07 · W4-M01

geom_jitter() · W4-M03
Add small position offsets to reveal overlapping points. Check argument names and data types before running the code.ggplot2::geom_jitter(width=...)

In the course: W4-M03

geom_line() · W4-M05
Connect points in x-axis order within groups. Check argument names and data types before running the code.ggplot2::geom_line()

In the course: W4-M05

geom_point() · W4-M02, W4-M05, W4-M06
Plot individual observations as points. Check argument names and data types before running the code.ggplot2::geom_point()

In the course: W4-M02 · W4-M05 · W4-M06

geom_violin() · W4-M03
Draw smoothed density shapes by group. Check argument names and data types before running the code.ggplot2::geom_violin()

In the course: W4-M03

ggplot() · W3-M07, W4-M01, W4-M02, W4-M03 …
Start a layered grammar-of-graphics plot. Check argument names and data types before running the code.ggplot2::ggplot(data, aes(...))

In the course: W3-M07 · W4-M01 · W4-M02 · W4-M03 · W4-M04 · W4-M05 · W4-M06

labs() · W3-M07, W4-M01, W4-M02, W4-M03 …
Set plot titles, axis labels and legend text. Check argument names and data types before running the code.ggplot2::labs(x="...", y="...", title="...")

In the course: W3-M07 · W4-M01 · W4-M02 · W4-M03 · W4-M05 · W4-M06

scale_x_continuous() · W3-M07
Control the continuous x-axis scale and tick marks. Check argument names and data types before running the code.ggplot2::scale_x_continuous(breaks=...)

In the course: W3-M07

theme_bw() · W4-M06
Apply a black-and-white plotting theme. Check argument names and data types before running the code.ggplot2::theme_bw()

In the course: W4-M06

theme_classic() · W3-M07, W4-M06
Apply a simple plotting theme with minimal background decoration. Check argument names and data types before running the code.ggplot2::theme_classic()

In the course: W3-M07 · W4-M06

Inference, testing and models (9)

abs() · W6-E01
Return the absolute value of each number. Check argument names and data types before running the code.abs(x)

In the course: W6-E01

all.equal() · W6-E02
Compare objects approximately, allowing small numeric tolerances. Check argument names and data types before running the code.all.equal(x, y)

In the course: W6-E02

cor() · W4-E01, W6-E03
Calculate a correlation coefficient, Pearson by default. Correlation does not establish causation.cor(x, y, use="complete.obs")

In the course: W4-E01 · W6-E03

lm() · W6-E03
Fit a linear statistical model using a formula. A simple linear model requires careful checks of its assumptions.lm(y ~ x, data=data)

In the course: W6-E03

p-value · W6-E01
Compute the two-sided tail probability under the null model.2 * pt(-abs(t), df)

In the course: W6-E01

pt() · W6-E01
Evaluate the cumulative probability for a Student t distribution. For a two-sided t test, account for both tails of the distribution.pt(t, df)

In the course: W6-E01

qt() · W6-E02
Get a quantile (critical value) of a Student t distribution. For a two-sided 95% CI, the upper reference quantile is 0.975.qt(.975, df)

In the course: W6-E02

summary() · W6-E03
Show a descriptive or model-specific statistical summary. Check argument names and data types before running the code.summary(model)

In the course: W6-E03

t.test() · W6-E01
Perform a Student t test and report a confidence interval. Check argument names and data types before running the code.t.test(x, mu=600)

In the course: W6-E01

Data import and inspection (8)

as.numeric() · W6-E02
Convert values to numeric form when conversion is meaningful. Check argument names and data types before running the code.as.numeric(x)

In the course: W6-E02

data.frame() · W1-E01
Build a rectangular data frame from named columns. Check argument names and data types before running the code.data.frame(x=..., y=...)

In the course: W1-E01

dim() · W1-M04, W3-M02
Return dimensions, usually rows and columns. Check argument names and data types before running the code.dim(x)

In the course: W1-M04 · W3-M02

head() · W1-M04, W2-M05, W2-M08, W3-M03 …
Show the first n elements or rows. Check argument names and data types before running the code.head(x, n=6)

In the course: W1-M04 · W2-M05 · W2-M08 · W3-M03 · W3-M04

names() · W1-M04, W2-M03
Get or assign column or element names. Check argument names and data types before running the code.names(data)

In the course: W1-M04 · W2-M03

nrow() · W2-M01, W2-M02, W3-M01, W3-M03 …
Count the number of rows in a table. Check argument names and data types before running the code.nrow(data)

In the course: W2-M01 · W2-M02 · W3-M01 · W3-M03 · W3-E01 · W4-E01

read.csv() · W2-M01, W3-M01, W4-E01
Read a comma-separated file into a data frame. Check argument names and data types before running the code.read.csv("data/file.csv", stringsAsFactors=FALSE)

In the course: W2-M01 · W3-M01 · W4-E01

table() · W2-M04, W2-M09
Count frequencies of unique values. Check argument names and data types before running the code.table(x)

In the course: W2-M04 · W2-M09

Simulation and base-R plotting (6)

hist() · W5-E03
Draw a Base R histogram. Check argument names and data types before running the code.hist(x, main="...", xlab="...")

In the course: W5-E03

par() · W5-E03
Set graphical parameters such as the plot-panel layout. Check argument names and data types before running the code.par(mfrow=c(1,2))

In the course: W5-E03

replace=TRUE · W5-E02
Allow an observed data point to appear more than once in a resample.sample(x, n, replace=TRUE)

In the course: W5-E02

replicate() · W5-E02
Evaluate an expression repeatedly and collect the results. Check argument names and data types before running the code.replicate(B, expression)

In the course: W5-E02

sample() · W5-E02
Draw elements with or without replacement. Bootstrap sampling usually uses replace=TRUE and the observed sample size.sample(x, size, replace=TRUE)

In the course: W5-E02

set.seed() · W5-E02
Fix the pseudorandom generator state for reproducible output. Check argument names and data types before running the code.set.seed(46)

In the course: W5-E02

Summary statistics and numeric calculations (9)

length() · W5-E01
Return the number of elements in a vector. Check argument names and data types before running the code.length(x)

In the course: W5-E01

max() · W1-M05, W3-M06
Return the largest value. Check argument names and data types before running the code.max(x, na.rm=FALSE)

In the course: W1-M05 · W3-M06

min() · W3-M06
Return the smallest value. Check argument names and data types before running the code.min(x, na.rm=FALSE)

In the course: W3-M06

quantile() · W5-E02
Calculate the requested percentiles of a vector. A percentile interval is only as credible as its resampling assumptions.quantile(x, probs=c(.025,.975))

In the course: W5-E02

sd() · W3-M06, W3-E01, W4-M04, W5-E01 …
Calculate the sample standard deviation. With the default sample definition, SD uses n−1 in the denominator.sd(x)

In the course: W3-M06 · W3-E01 · W4-M04 · W5-E01 · W5-E02

sqrt() · W3-E01, W5-E01, W6-E01
Calculate the non-negative square root. Check argument names and data types before running the code.sqrt(x)

In the course: W3-E01 · W5-E01 · W6-E01

sum() · W1-M02, W3-M04, W3-M05, W3-M06
Add numeric values; optionally ignore missing values. Check argument names and data types before running the code.sum(x, na.rm=FALSE)

In the course: W1-M02 · W3-M04 · W3-M05 · W3-M06

var() · W3-E01
Calculate the sample variance. Variance has squared measurement units.var(x)

In the course: W3-E01