How to Use lapply, sapply and mapply in R
This video shows how to use the lapply, sapply and mapply functions to execute a function on each element of a list or vector in R. The apply family of functions provide a convenient way to quickly invoke a function on each element of vectors without having to write custom for loops. Note that since data frames are lists where each column are separate elements of the list, the lapply and sapply will apply a function to each column when run on a data frame. This provides a quick way to create column-based summary statistics. Code used in this clip: Example of lapply data <- mtcars Function to apply mpg_category <- function(mpg){ if(mpg > 30){ return("High") } else if (mpg > 20){ return("Medium") } return("Low") } Apply to each element lapply(X = data$mpg, FUN = mpg_category) Use sapply to simplify the result to a vector or matrix instead of a list sapply(X = data$mpg, FUN = mpg_category) You can pass additional arguments after FUN within_range <- function(mpg, low, high){ if (mpg >= low & mpg <= high){ return(TRUE) } return(FALSE) } index <- sapply(X = data$mpg, FUN = within_range, low = 15, high = 20) index data[index,] Use mapply to apply a function along multiple vectors at the same time Function to apply mpg_within_standard_range <- function(mpg, cyl){ if (cyl == 4){ return(within_range(mpg, low = 23, high = 31)) } else if (cyl == 6) { return(within_range(mpg, low = 18, high = 23)) } return(within_range(mpg, low = 13, high = 18)) } index <- mapply(FUN = mpg_within_standard_range, mpg = data$mpg, cyl = data$cyl) index data[!index,] When used on a dataframe, lapply and sapply apply a function to each column sapply(data, FUN = median) Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! For R that means I may use = for assignment and the special Unicode large < and > symbols in place of the standard sized ones for dplyr pipes and comparisons. These special symbols should work as expected for R code on Windows, but may need to be replaced with standard greater than and less than symbols for other operating systems.

How To Use Apply in R

A tutorial for writing functions in R (CC177)

Apply Family of Functions in R Part 1: apply()

Data wrangling with R in 27 minutes

Apply Functions in R | lapply() | sapply() | mapply() | tapply() in R | Iteration without loops in R

Learn R Programming for Data Analysis | Full Beginner's Course | A to Z

Hands-on dplyr tutorial for faster data manipulation in R

Learn R in 39 minutes

Map functions in purrr (R Tidyverse)

The across( ) function in R programming. A tidyverse function to make cleaning data easy.

For Loops in R | Further Data Analysis with R (Lesson 7)

R tutorial - Using Factors in R

Writing Your Own Functions in R: Introduction

Working with lists in R

Describe and Summarise your data

R programming for beginners: using functions and objects in R

Teaching the tidyverse in 2023 | Mine Çetinkaya-Rundel

Dplyr Essentials (easy data manipulation in R): select, mutate, filter, group_by, summarise, & more

sweep Function in R (3 Examples) | Matrix Operation with MARGIN & STATS | Sweep out Array Summaries

