Skip to content
Justice Tech School

Apply Functions

apply, sapply, lapply and vapply run a function over elements without writing loops. sapply simplifies results to a vector.

Example
x <- 1:5
print(sapply(x, function(n) n^2))
print(lapply(1:2, function(n) n * 10))
m <- matrix(1:6, 2); print(apply(m, 1, sum))

Exercise

Run a function over a vector.

(x, sqrt)

Quiz Test

Which simplifies results to a vector?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.