In this laboratory you will focus on R as a programming language. You
will practice writing your own functions (including simple wrappers
around existing functions), doing basic linear algebra, using
for loops, and working with lists and simple functional
programming tools.
Throughout this instruction, type the examples into an R script and run them. Modify the code and experiment to see how R behaves.
In R, functions are defined with function(...) and
usually stored in variables:
# A simple function: compute the mean of a numeric vector,
# ignoring missing values
my_mean <- function(x) {
mean(x, na.rm = TRUE)
}
values <- c(1, 2, 3, NA, 5)
my_mean(values)Task 1.1
my_range(x) that returns the
difference between the maximum and minimum of x.x contains NA values (use
na.rm = TRUE inside your function).Very often we write small wrapper functions that call an existing function but: - set sensible default values for arguments, or - pass only a subset of arguments in a convenient way.
# A wrapper around the built‑in plot() function
my_plot <- function(x, y,
xlab = "x", ylab = "y",
main = "My line plot", ...) {
plot(x, y,
type = "l", # line plot
xlab = xlab,
ylab = ylab,
main = main,
...)
}
t <- seq(0, 2 * pi, length.out = 200)
y <- sin(t)
my_plot(t, y, main = "Sine wave")Notice the ... (ellipsis) argument: it allows you to
pass any extra arguments through to plot(), such as
col, lwd, ylim, etc.
Task 1.2
my_plot() so that it has an additional argument
grid = TRUE.grid is TRUE, the function should call
grid() after plotting (which adds a simple background grid
to the plot).cos(t) in blue (col = "blue"),exp(-t) in red (col = "red").Write a wrapper that combines a numeric summary and a simple plot:
quick_summary_plot <- function(x, main = "Quick summary plot") {
cat("Summary of x:\n")
print(summary(x))
hist(x,
breaks = 20,
main = main,
xlab = "x",
col = "lightgray")
}
set.seed(123)
z <- rnorm(200, mean = 10, sd = 3)
quick_summary_plot(z, main = "Normal data")Task 1.3
Create your own wrapper quick_boxplot(x, group) that: 1.
Prints the number of observations in each group (use
table(group)), 2. Draws a boxplot of x grouped
by group using boxplot(x ~ group).
Test it on:
R was originally designed for statistical computing and handles matrices and linear algebra very well.
# Create a 3x3 matrix filled by column
A <- matrix(c(1, 2, 3,
4, 5, 6,
7, 8, 9),
nrow = 3, ncol = 3, byrow = FALSE)
# Create a column vector
b <- c(1, 0, 1)
A
b
# Matrix‑vector multiplication
A %*% b
# Transpose
t(A)Task 2.1
4 x 4 matrix M with entries from
1 to 16 (use matrix(1:16, nrow = 4, byrow = TRUE)).v <- c(1, 2, 3, 4) and compute
M %*% v.t(M) %*% v. Compare the results.To solve the system \(A x = b\) we
use the solve() function.
A <- matrix(c(2, 1,
1, 3),
nrow = 2, byrow = TRUE)
b <- c(1, 2)
# Solve A x = b
x <- solve(A, b)
x
# Check the result
A %*% xTask 2.2
3 x 3 matrix B and a vector
d of length 3 such that the system B x = d has
a unique solution.B with a non‑zero determinant, for example a
triangular matrix.)solve(B, d) to find x, and verify the
result by computing B %*% x.for loops in Rfor loopTask 3.1
for loop that computes the sum of squares \(\sum_{i=1}^{n} i^2\) for a given
n.sum((1:n)^2) to check
correctness.Task 3.2
M of size n x n
(use matrix(0, n, n)).for loops to fill M[i, j] with
the value i * j.outer(1:n, 1:n, "*").Lists in R can store objects of different types and sizes. They are the main “container” structure in many R packages.
student <- list(
name = "Alice",
age = 21,
scores = c(80, 90, 85)
)
student$name
student$scores
student[["age"]]Task 4.1
course with elements:
title – a character string,ects – a numeric value,students – a vector of student names.$ operator and using
double square brackets [[ ]].students <- list(
list(name = "Alice", scores = c(80, 90, 85)),
list(name = "Bob", scores = c(70, 75, 72)),
list(name = "Carol", scores = c(95, 92, 98))
)
students[[1]]$name
students[[2]]$scoresTask 4.2
students list above, compute the average score
for each student using a for loop and store the results in
a numeric vector.sapply() (see
below).lapply and sapplyThe functions lapply() and sapply() apply a
function to each element of a list:
# Average scores for each student (list of students)
avg_scores <- sapply(
students,
function(s) mean(s$scores)
)
avg_scoresTask 4.3
X containing three numeric vectors of
different lengths (for example, rnorm(5),
runif(10), rpois(7, lambda = 3)).lapply(X, mean) to compute the mean of each
vector.sapply(X, length) to compute the length of each
vector.Put everything together in a small programming task.
Task 5.1
Write a function simulate_means(n, m) that:
m samples, each of size n, from
a standard normal distribution (use rnorm(n)).m.sapply() to compute the mean of each sample.Then:
set.seed(42)
result <- simulate_means(n = 30, m = 1000)
str(result)
hist(result$means,
breaks = 30,
main = "Distribution of sample means",
xlab = "Sample mean")Comment briefly on the shape of the histogram of sample means.