Hamburger Icon
  • Labs icon Lab
  • Data
Labs

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.

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 24m
Published
Clock icon Feb 21, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge ourΒ privacy policy.

Table of Contents

  1. 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 of apply in a simple use case using the sapply function.


    Task 1.1: Create a Simple Function

    Create a function named square that takes an argument x and returns the square of x.

    πŸ” Hint

    Use the ^ operator to calculate the square of x.

    πŸ”‘ 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 the square function to each element in the numbers 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 the squared_numbers vector.

    πŸ”‘ Solution
    print(squared_numbers)
    
  2. 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 into apply, 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. Using sapply(), apply an anonymous function to numbers that multipies each element by 3.

    πŸ” Hint

    Use the : operator to create a sequence of numbers, and assign this to a variable called numbers.
    Then call the sapply() function. The first argument should be numbers, 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 the data() function to load the mtcars dataset into the environment.

    Using the apply() function, apply an anonymous function to the mtcars data frame that computes the mean of each column.

    πŸ” Hint

    Use the apply function with mtcars 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 the mtcars 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 the mean, median, and sum functions to compute the values. Use print() for the mean and median, and return() 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`  
    
  3. 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, and vapply 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, and vapply 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 the mean function to each vector in the provided list.

    πŸ” Hint

    Use lapply function with my_list as the first argument and mean 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 the mean function to each vector in the list.

    πŸ” Hint

    Use sapply function with my_list and mean as arguments.

    πŸ”‘ Solution
    sapply_result <- sapply(my_list, mean)
    

    Task 3.3: Using the vapply Function

    Finally, use the vapply function to apply the mean function to each vector in the list. Remember that vapply 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 with my_list and mean as the first two arguments. The third argument is a function that specifies the type of output you expect (e.g., numeric(), logical(), or character()), 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))
    
  4. 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 and replicate is important because these functions offer unique functionalities in R. tapply allows you to group and apply a function over a specific group, while replicate helps in creating repeatable experiments.

    In this step, we will practice using the tapply and replicate functions in R. We will understand how tapply applies a function over groups and how replicate 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 the iris dataset into your environment.

    Inspect the structure of the iris dataset and read through the iris help page.

    πŸ” Hint

    Use the library() function to load the datasets package. Then use the data() function to load the iris dataset into your environment.
    You can inspect the structure of a data frame using str(), 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 of Sepal.Length for each Species in the iris dataset.

    πŸ” Hint

    The first argument should specify the Sepal.Length column to indicate the data you wish to analyze. For the second argument, use the Species 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 is runif() 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.