- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- 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.
Lab 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
sapplyfunction 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 ofapplyin a simple use case using thesapplyfunction.
Task 1.1: Create a Simple Function
Create a function named
squarethat takes an argumentxand 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
numberswith 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
sapplyfunction to apply thesquarefunction to each element in thenumbersvector.🔍 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_numbersvector to the console.🔍 Hint
Use the
printfunction to print thesquared_numbersvector.🔑 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
applyfunction 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
numbersthat contains the values 1 through 10. Usingsapply(), apply an anonymous function tonumbersthat 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
datasetspackage and use thedata()function to load themtcarsdataset into the environment.Using the
apply()function, apply an anonymous function to themtcarsdata frame that computes the mean of each column.🔍 Hint
Use the
applyfunction withmtcarsas the first argument,2as 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 themtcarsdata 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, andsumfunctions 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, andvapplyis 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, andvapplyfunctions 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
lapplyfunction to apply themeanfunction to each vector in the provided list.🔍 Hint
Use
lapplyfunction withmy_listas the first argument andmeanas 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
sapplyfunction to apply themeanfunction to each vector in the list.🔍 Hint
Use
sapplyfunction withmy_listandmeanas arguments.🔑 Solution
sapply_result <- sapply(my_list, mean)
Task 3.3: Using the vapply Function
Finally, use the
vapplyfunction to apply themeanfunction to each vector in the list. Remember thatvapplytakes a third argument to specify the expected output type.First, use
vapplywhile specifying the correct output type.Then, specify an incorrect output type and verify that
vapplyraises an error message.🔍 Hint
Use the
vapplyfunction withmy_listandmeanas 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
tapplyandreplicateis important because these functions offer unique functionalities in R.tapplyallows you to group and apply a function over a specific group, whilereplicatehelps in creating repeatable experiments.In this step, we will practice using the
tapplyandreplicatefunctions in R. We will understand howtapplyapplies a function over groups and howreplicatecan 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
datasetslibrary and load theirisdataset into your environment.Inspect the structure of the
irisdataset and read through theirishelp page.🔍 Hint
Use the
library()function to load thedatasetspackage. Then use thedata()function to load theirisdataset 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
tapplyfunction to calculate the mean ofSepal.Lengthfor eachSpeciesin theirisdataset.🔍 Hint
The first argument should specify the
Sepal.Lengthcolumn to indicate the data you wish to analyze. For the second argument, use theSpeciescolumn 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
replicatefunction 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))
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.