CodeNFacts
CodeHub
Home

All Categories


Sign In
RStatistical computing language

Learn R the way statisticians actually think.

R turns "loop over the data" into "describe what should happen to the data" — a small mental shift that changes how you build logic, not just what syntax you use. This page is a full working reference: why R exists, how it reshapes your coding habits, how model training actually works in R, plus deep-dive notes, a cheat sheet, practice problems, and a quiz.

R 4.4.1 — console
> x <- c(4, 8, 15, 16, 23, 42)
> mean(x)
[1] 18

> model <- lm(mpg ~ wt + hp, data = mtcars)
> summary(model)$r.squared
[1] 0.8268

> library(ggplot2)
> ggplot(mtcars, aes(wt, mpg)) + geom_point()
Why R, specifically

Why you'd reach for R instead of a general-purpose language

Statistics is native, not bolted on

Distributions, hypothesis tests, and regression are base-language functions, not third-party add-ons — the vocabulary of statistics is R's own vocabulary.

Best-in-class visualization

ggplot2's layered grammar of graphics is still the reference point competing tools measure themselves against for exploratory and publication-quality plots.

A CRAN package for almost everything

20,000+ peer-checked packages cover niche statistical methods that would otherwise take weeks to implement from a paper.

Reproducible by default

R Markdown/Quarto ties code, output, and narrative into one document — the standard for reproducible research in academia and applied data science.

How R changes how you build logic

From "loop over it" to "describe what should happen to it"

Most people learn programming through explicit loops: walk through a list, do something to each item. R nudges you toward a different default — vectorized and functional thinking, where you describe the transformation once and let it apply across the whole structure. That habit carries over into every language you touch afterward.

Mental habitTypical imperative styleR's default style
Doubling every numberfor i in list: out.append(i*2)x * 2
Filtering rowsif condition: keep.append(row)filter(df, condition)
Applying a function to every itemfor item in items: f(item)sapply(items, f)
Grouped aggregationmanual dict of running sumsgroup_by(df, g) |> summarise(...)

The practical effect: R developers tend to write shorter functions, think in terms of whole columns/vectors rather than single values, and lean on existing statistical/functional building blocks instead of re-deriving control-flow logic from scratch. That's a transferable habit — it shows up later in pandas (Python), Spark, and SQL, all of which reward the same "operate on the whole set" thinking.

Training models in R

The workflow you'll repeat for almost every model

  1. 01

    Split your data

    set.seed() for reproducibility, then hold out a test set (commonly 20–30%) with rsample::initial_split() or plain sample().

  2. 02

    Preprocess

    Centre/scale numeric predictors, encode categorical variables, impute or drop missing values — recipes (tidymodels) or caret::preProcess().

  3. 03

    Choose & fit a model

    Classical: lm()/glm(). Tree-based: randomForest, xgboost. Unified interface: a parsnip model spec from tidymodels.

  4. 04

    Cross-validate & tune

    k-fold CV (vfold_cv() or caret::trainControl(method='cv')) estimates performance honestly and tune_grid()/train() search hyperparameters.

  5. 05

    Evaluate once, on the test set

    RMSE/MAE for regression, accuracy/precision/recall/AUC for classification — computed only after tuning is finished.

  6. 06

    Explain & ship

    Wrap the fitted model + report in an R Markdown/Quarto document, or serve predictions through a Shiny app or plumber API.

Deep-dive notes

Every topic you need to actually know R

  • R is a language + environment for statistical computing and graphics, born out of the S language at Bell Labs.
  • Distributed as 'base R', extended by 20,000+ packages hosted on CRAN (the Comprehensive R Archive Network).
  • RStudio / Posit is the IDE almost everyone uses — console, script editor, plots, and environment viewer in one window.
  • R is interpreted and interactive: you run one line, see the result immediately, then build on it. That loop is the whole point.
Quick reference

R cheat sheet

Vectors & basics
c(1, 2, 3)Combine values into a vector
seq(1, 10, by = 2)Sequence with a step
rep(x, times = 3)Repeat a vector
length(x)Number of elements
x[c(1,3)]Subset by position
x[x > 5]Subset by logical condition
Data frames
str(df)Structure / column types
head(df, 5)First 5 rows
df[df$score > 80, ]Filter rows (base R)
df$new <- df$a + df$bAdd a computed column
nrow(df); ncol(df)Dimensions
colnames(df)Column names
dplyr verbs
filter(df, x > 10)Keep matching rows
select(df, a, b)Keep specific columns
mutate(df, z = a * b)Add/modify a column
arrange(df, desc(x))Sort rows
group_by(df, g) |> summarise(m = mean(x))Aggregate per group
left_join(a, b, by = "id")Join two tables
Stats & distributions
mean(x); median(x); sd(x)Central tendency & spread
summary(x)Quick 5-number summary
cor(x, y)Correlation
rnorm(n, mean, sd)Random normal draws
t.test(x, y)Compare two means
lm(y ~ x, data = df)Fit a linear model
Plotting (ggplot2)
ggplot(df, aes(x, y))Start a plot, map variables
+ geom_point()Scatter layer
+ geom_line()Line layer
+ geom_boxplot()Boxplot layer
+ facet_wrap(~group)Small multiples by group
+ labs(title = "...")Titles & axis labels
Control & functions
if (x > 0) { ... } else { ... }Branching
for (i in seq_along(x)) { ... }Loop by index (rare in idiomatic R)
function(x, y = 1) { x + y }Define a function, default arg
sapply(x, f)Apply f() over every element
ifelse(cond, a, b)Vectorized if/else
Practice

Problems to actually write R

Beginner

Vector clean-up

Given `x <- c(4, NA, 8, NA, 15, 16)`, write one line that returns the mean of x, ignoring the missing values.

Beginner

FizzBuzz, the R way

Print numbers 1 to 20; multiples of 3 print 'Fizz', multiples of 5 print 'Buzz', multiples of both print 'FizzBuzz'.

Intermediate

Group summary

Using the built-in `mtcars` dataset, find the average `mpg` for each number of cylinders (`cyl`), sorted from highest to lowest.

Intermediate

Write your own apply

Write a function `col_means(df)` that returns the mean of every numeric column in a data frame, without a `for` loop.

Intermediate

String cleanup

You have `names <- c(" Alice ", "BOB", "charlie ")`. Return a vector with whitespace trimmed and consistent Title Case.

Advanced

Train/test split + logistic regression

Using a data frame `df` with a binary column `outcome`, split it 80/20, fit a logistic regression on the training set, and report accuracy on the test set.

Advanced

Cross-validated RMSE

Using tidymodels (`rsample`), perform 5-fold cross-validation on a linear model predicting `mpg` from `wt` and `hp` in `mtcars`, and report the average RMSE across folds.

Check yourself

Quick R quiz

1.What does `<-` do in R?

2.What will `class(42L)` return?

3.Which structure can hold a number, a string, and a data frame all in one object?

4.Why is `for` looping over a vector often discouraged in R?

5.What does the pipe `|>` (or `%>%`) do?

6.In `lm(y ~ x, data = df)`, what does the `~` mean?

7.A factor stores its categories internally as:

8.Which function family applies another function across every element of a list/vector without writing an explicit loop?

9.What's the recommended reason to evaluate a model on a held-out test set only once, at the end?

10.What does `ggplot2`'s 'grammar of graphics' approach mean in practice?

Keep this page as your reference while you practice — or grab the notes offline for the console session next to you.