How to Use lapply, sapply and mapply in R @DataDaft
How to Use lapply, sapply and mapply in R  @DataDaft
Uploaded February 2020 | Updated November 2026, 3 hours ago
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 lapply, sapply and mapply in RHow to Select Columns Based on a Logical Condition in Pandas (Python)Python for Data Analysis: Descriptive Statisticsdplyr: JoinsAlgorithms Explained: Computational ComplexityPython for Data Analysis: Confidence IntervalsIntroduction to R: T-Tests (Hypothesis Testing)How to Select a Column in RPython Programming Practice: LeetCode #6   ZigZag ConversionSongs of the 90s: All #1 HitsCOVID-19 New Cases UPDATED (March 20)How to Get Unique (Distinct) Values of a Column in Pandas (Python)
DataDaft |

How to Use lapply, sapply and mapply in R

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER