R Exercises
Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.
R Introduction
Exercise
Print a value.
("Hi")R Get Started
Exercise
Run a script.
hello.RR Syntax
Exercise
Assignment arrow.
x 5
R Comments
Exercise
Start a comment.
noteR Variables
Exercise
Assign a value.
n 10
R Data Types
Exercise
Show the type.
(x)R Numbers
Exercise
Remainder operator.
17 5
R Strings
Exercise
Join without a space.
("a", "b")R Logicals
Exercise
Logical and.
a b
R Operators
Exercise
Membership test.
3 x
R Math Functions
Exercise
Running total.
(x)Sequences and Sorting
Exercise
Reverse order sort.
sort(x, = TRUE)
R Output and Formatting
Exercise
Format with decimals.
("%.1f", 2.55)Assignment
Exercise
Assign.
name "Ada"
Basic Types
Exercise
Integer literal.
n <- 5Vectors
Exercise
Combine values.
v <- (1, 2, 3)
Vector Arithmetic
Exercise
Whole vector math.
a b
Sequences
Exercise
Step by 2.
seq(0, 10, = 2)
Indexing Vectors
Exercise
Drop the first.
v[1]
NA, NULL and Missing Values
Exercise
Ignore NA.
sum(x, = TRUE)
Strings
Exercise
Join without a gap.
("a", "b")Searching and Replacing Text
Exercise
Replace all.
("a", "b", x)Math Functions
Exercise
Round to 2 places.
(x, 2)Factors
Exercise
Count categories.
(f)Dates
Exercise
Convert text.
.Date("2026-01-01")R If Else
Exercise
Vector friendly if.
(x > 1, "a", "b")R Switch
Exercise
Make 2 the default value.
switch(x, a = 1, )
R For and While Loops
Exercise
Loop over 1 to 3.
for (i 1:3)
R Functions
Exercise
Define a function.
add <- (a, b) a + b
R Scope and Closures
Exercise
Assign to an outer variable.
n n + 1
R Repeat and Break
Exercise
Leave the loop.
if (x) if and else
Exercise
Vectorised if.
(cond, "a", "b")for and while Loops
Exercise
Skip a round.
repeat and switch
Exercise
Infinite loop.
{ break }Vectorising Instead of Looping
Exercise
Running total.
(x)Functions
Exercise
Default argument.
function(a, b 1)
Returning Several Values
Exercise
Return many.
(a = 1, b = 2)Scope and Closures
Exercise
Assign outside.
n n + 1
The Pipe Operator
Exercise
Pass x into sqrt.
x |> ()
Errors and tryCatch
Exercise
Raise an error.
("message")Recursion
Exercise
Base case.
if (n <= ) 1
R Vectors
Exercise
First index in R.
x[]
R Lists
Exercise
Read an element by name.
pname
R Matrices
Exercise
Matrix size.
(m)R Arrays and Factors
Exercise
Categorical type.
(c("a", "b"))R Data Frames
Exercise
Create a table.
df <- (a = 1:3)
Apply Functions
Exercise
Run a function over a vector.
(x, sqrt)Set Operations
Exercise
Common elements.
(a, b)Lists
Exercise
Reach an item.
lname
Matrices
Exercise
Matrix size.
(m)Arrays and apply
Exercise
Rows.
apply(m, , sum)
Data Frames
Exercise
Build a table.
.frame(a = 1:2)Subsetting Data Frames
Exercise
Filter rows.
df[df$score 80, ]
Adding and Changing Columns
Exercise
Remove a column.
df$b <- Summaries of Data Frames
Exercise
Quick summary.
(df$v)The apply Family
Exercise
Simplify result.
(x, sum)seq_along and Loop Helpers
Exercise
Safe indexes.
for (i in (x))
Sorting and Ordering
Exercise
Positions.
(x)Set Operations
Exercise
Membership.
a b
Importing Data
Exercise
Read a CSV.
("data.csv")Cleaning Data
Exercise
Ignore NA in a mean.
mean(x, = TRUE)
Data Wrangling with dplyr
Exercise
Keep matching rows.
df |> (v > 1)
Dates and Regular Expressions
Exercise
Convert to a date.
("2026-01-01")Reading and Writing CSV
Exercise
Write a file.
.csv(df, "f.csv")Merging Tables
Exercise
Join key.
merge(a, b, = "id")
aggregate and by-group Summaries
Exercise
Group summary.
(s ~ c, df, mean)Sorting a Data Frame
Exercise
Descending.
df[order(df$s), ]
Frequency Tables
Exercise
Proportions.
.table(t)Binning with cut
Exercise
Intervals.
(s, breaks = b)Applying Functions to Columns
Exercise
Per column.
(df, mean)Duplicates and Unique Rows
Exercise
Remove repeats.
(x)Handling Missing Data
Exercise
Drop rows with NA.
.omit(df)Formatting Output
Exercise
Thousands separator.
format(x, big.mark = "")
Descriptive Statistics
Exercise
Middle value.
(x)Probability Distributions
Exercise
Repeatable randomness.
.seed(42)Correlation and Linear Regression
Exercise
Fit a linear model.
m <- (y ~ x)
Hypothesis Tests
Exercise
Compare two means.
.test(a, b)Descriptive Statistics
Exercise
Spread.
(x)Correlation
Exercise
Correlation.
(x, y)Linear Regression
Exercise
Fit a model.
(y ~ x)Probability Distributions
Exercise
Random normal.
(10)Sampling and Seeds
Exercise
Repeatable.
.seed(42)Hypothesis Tests
Exercise
Compare means.
.test(a, b)Scaling and Cumulative Values
Exercise
Standardise.
(x)Matrix Statistics
Exercise
Column totals.
(m)Finding Outliers
Exercise
Outliers.
boxplot.stats(x)$Base R Plots
Exercise
Draw a histogram.
(x)ggplot2
Exercise
Add a layer with.
ggplot(df, aes(x, y)) geom_point()
Choosing Charts
Exercise
Chart for a trend over time.
Use a chart
Base Plots
Exercise
Plot points.
(x, y)Histograms and Boxplots
Exercise
Distribution.
(x)Bar Charts
Exercise
Bars.
(counts)Multiple Plots
Exercise
Grid of plots.
par( = c(1, 2))
ggplot2 Introduction
Exercise
Add a layer.
ggplot(df, aes(x, y)) geom_point()
ggplot2 Geoms
Exercise
Bar chart layer.
geom_
Facets, Colours and Themes
Exercise
Small multiples.
facet_(~ g)
Saving Plots
Exercise
Close the device.
.off()Practice: Word Frequency
Exercise
Count words.
(w)Practice: FizzBuzz
Exercise
Remainder.
i 3
Practice: Class Report
Exercise
Highest row.
.max(df$score)Object Oriented R
Premium exercise. See plans
Functional Programming
Premium exercise. See plans
Error Handling
Premium exercise. See plans
Vectorisation and Performance
Premium exercise. See plans
Packages and Projects
Premium exercise. See plans
R Markdown and Quarto
Premium exercise. See plans
Shiny Apps
Premium exercise. See plans
Databases from R
Premium exercise. See plans
Web APIs and JSON
Premium exercise. See plans
Machine Learning Basics
Premium exercise. See plans
Reshaping with tidyr
Premium exercise. See plans
R Best Practices
Premium exercise. See plans