- Lab
- Data

Applying Functions in R Hands-on Practice
This lab teaches functional programming with the apply function. It covers vectorizing operations using sapply, creating and applying anonymous functions, and using common functions like lapply and sapply. You'll also learn advanced functions like tapply for grouped operations and replicate for repeatable experiments.

Path Info
Table of Contents
-
Challenge
Understanding Functional Programming and Apply
RStudio Guide
To get started, click on the 'workspace' folder in the bottom right pane of RStudio. Click on the file entitled "Step 1...". You may want to drag the console pane to be smaller so that you have more room to work. You'll complete each task for Step 1 in that R Markdown file. Remember, you must run the cells with the play button at the top right of each cell for a task before moving onto the next task in the R Markdown file. Continue until you have completed all tasks in this step. Then when you are ready to move onto the next step, you'll come back and click on the file for the next step until you have completed all tasks in all steps of the lab.
Understanding Functional Programming and Apply
To review the concepts covered in this step, please refer to the Understanding Functional Programming and Apply module of the Applying Functions in R course.
Understanding Functional Programming and the use of Apply function is important because it helps you write efficient and readable code in R. It introduces you to the concept of vectorization and why it is preferred over for loops in R.
In this step, we will practice the functional approach to programming in R. We will create a function and use the
sapply
function to call it across a vector. This will help us understand how vectorizing operations can be faster and more efficient than using for loops in R. The goal here is to get comfortable with the concept ofapply
in a simple use case using thesapply
function.
Task 1.1: Create a Simple Function
Create a function named
square
that takes an argumentx
and returns the square ofx
.π Hint
Use the
^
operator to calculate the square ofx
.π Solution
square <- function(x) { return(x^2) }
Task 1.2: Create a Vector
Create a numeric vector
numbers
with values from 1 to 10.π Hint
Use the
:
operator to create a sequence of numbers.π Solution
numbers <- 1:10
Task 1.3: Apply the Function to the Vector
Use the
sapply
function to apply thesquare
function to each element in thenumbers
vector.π Hint
Call the
sapply()
function on the vector. The first argument should be the vector and the second argument should be the function.π Solution
squared_numbers <- sapply(numbers, square)
Task 1.4: Print the Result
Print the
squared_numbers
vector to the console.π Hint
Use the
print
function to print thesquared_numbers
vector.π Solution
print(squared_numbers)
-
Challenge
Working with Anonymous Functions in Apply
Working with Anonymous Functions in Apply
To review the concepts covered in this step, please refer to the Adding Anonymous Functions to Apply module of the Applying Functions in R course.
Understanding how to use Anonymous Functions with Apply is important because it can make your code more clear and concise. It also helps you avoid some common pitfalls associated with anonymous functions.
In this step, we will practice creating and using anonymous functions in R. We will use these anonymous functions with the
apply
function to perform operations on a data frame. The goal here is to understand the basics of anonymous functions, how to pass them intoapply
, and when to use a named function instead.
Task 2.1: Using an Anonymous Function
Create a vector named
numbers
that contains the values 1 through 10. Usingsapply()
, apply an anonymous function tonumbers
that multipies each element by 3.π Hint
Use the
:
operator to create a sequence of numbers, and assign this to a variable callednumbers
.
Then call thesapply()
function. The first argument should benumbers
, and the second argument should be a function.
Note: If a function is contained on a single line, curly braces{}
are unnecessary.π Solution
numbers <- 1:10 sapply(numbers, function(x) x*3)
Task 2.2: Applying an Anonymous Function to a Data Frame
Load the
datasets
package and use thedata()
function to load themtcars
dataset into the environment.Using the
apply()
function, apply an anonymous function to themtcars
data frame that computes the mean of each column.π Hint
Use the
apply
function withmtcars
as the first argument,2
as the second argument to indicate that you want to apply the function to each column, and an anonymous function as the third argument.π Solution
library('datasets') data('mtcars') apply(mtcars, 2, function(x) mean(x)) # because `mean` is itself a function, this solution is also valid apply(mtcars, 2, mean)
Task 2.3: Creating a Multi-Line Anonymous Function
Using the
apply()
function, apply a multi-line anonymous function to themtcars
data frame that prints the mean and median of each column and then returns the sum.π Hint
Use curly braces
{}
to make the anonymous function span several lines. Use themean
,median
, andsum
functions to compute the values. Useprint()
for the mean and median, andreturn()
for the sum.π Solution
library('datasets') apply(mtcars, 2, function(x) { print(mean(x)) print(median(x)) return(sum(x)) })
Task 2.4: When to Use a Named Function Instead of an Anonymous Function
For each of the three functions below, decide whether it would be more appropriate to use a named or anonymous function.
# Function 1: function(n) matrix(c(1,1,1,0),2,2)^n[1,2] # Function 2: function(x) x - mean(x) # Function 3: function(x) { mean_val <- mean(x) sd_val <- sd(x) z_score <- (x - mean_val) / sd_val return(z_score) }
π Hint
It is more appropriate to use a named function if the function spans multiple lines, takes more than a few seconds to understand, or if you intend to use the function many times.
π Solution
# Function 1: Named # This function, which computes the Fibonacci sequence up # to the nth term, is short but difficult to understand quickly # Function 2: Anonymous # This function is short and easy to understand # Function 3: Named # This function spans multiple lines and would # be more appropriate as a named function called `z_score`
-
Challenge
Exploring Common Apply Functions: lapply, sapply, and vapply
Exploring Common Apply Functions: lapply, sapply, and vapply
To review the concepts covered in this step, please refer to the Using Common Apply Functions module of the Applying Functions in R course.
Understanding the differences and use cases of
lapply
,sapply
, andvapply
is important because these functions offer different ways to apply a function over a list or vector in R. They each have their own benefits and specific use cases.In this step, we will practice using the
lapply
,sapply
, andvapply
functions in R. We will understand how these functions iterate over a list or vector and how their outputs differ. The goal here is to get comfortable with these functions and understand when to use each one.
Task 3.1: Using the lapply Function
Use the
lapply
function to apply themean
function to each vector in the provided list.π Hint
Use
lapply
function withmy_list
as the first argument andmean
as the second argument.π Solution
my_list <- list(a = c(1, 2, 3, 4, 5), b = c(6, 7, 8, 9, 10)) lapply(my_list, mean)
Task 3.2: Using the sapply Function
Now, use the
sapply
function to apply themean
function to each vector in the list.π Hint
Use
sapply
function withmy_list
andmean
as arguments.π Solution
sapply_result <- sapply(my_list, mean)
Task 3.3: Using the vapply Function
Finally, use the
vapply
function to apply themean
function to each vector in the list. Remember thatvapply
takes a third argument to specify the expected output type.First, use
vapply
while specifying the correct output type.Then, specify an incorrect output type and verify that
vapply
raises an error message.π Hint
Use the
vapply
function withmy_list
andmean
as the first two arguments. The third argument is a function that specifies the type of output you expect (e.g.,numeric()
,logical()
, orcharacter()
), and an argument given to that function specifies the length of output you expect.π Solution
vapply(my_list, mean, numeric(1)) # examples of an incorrect type vapply(my_list, mean, numeric(5)) vapply(my_list, mean, character(1))
-
Challenge
Using and Understanding Which Function to Use: tapply and replicate
Using and Understanding Which Function to Use: tapply and replicate
To review the concepts covered in this step, please refer to the Using and Understanding Which Function to Use module of the Applying Functions in R course.
Understanding when to use
tapply
andreplicate
is important because these functions offer unique functionalities in R.tapply
allows you to group and apply a function over a specific group, whilereplicate
helps in creating repeatable experiments.In this step, we will practice using the
tapply
andreplicate
functions in R. We will understand howtapply
applies a function over groups and howreplicate
can be used to iterate over a function. The goal here is to understand the use cases of these functions and when to use each one.
Task 4.1: Load and Examine the Dataset
Load the pre-installed
datasets
library and load theiris
dataset into your environment.Inspect the structure of the
iris
dataset and read through theiris
help page.π Hint
Use the
library()
function to load thedatasets
package. Then use thedata()
function to load theiris
dataset into your environment.
You can inspect the structure of a data frame usingstr()
, and can use the?
operator to bring up the help page for a function or dataset.π Solution
library('datasets') data(iris) str(iris) ?iris
Task 4.2: Using the tapply function
Use the
tapply
function to calculate the mean ofSepal.Length
for eachSpecies
in theiris
dataset.π Hint
The first argument should specify the
Sepal.Length
column to indicate the data you wish to analyze. For the second argument, use theSpecies
column to define the grouping categories. Lastly, the third argument should be the function you intend to apply to each group.π Solution
tapply(iris$Sepal.Length, iris$Species, mean)
Task 4.2: Using the replicate function
Use the
replicate
function to generate 5 random uniform distributions. Each distribution should have a minimum of 0, a maximum of 1, and should contain 10 values.π Hint
Use the
replicate()
function with the number of replications as the first argument, and the function to replicate as the second argument. The function to replicate in this case isrunif()
with arguments 10, 0, and 1.π Solution
replicate(5, runif(10, 0, 1))
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the authorβs guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.