Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Introduction to apply, lapply, and sapply | Apply Family Functions in R
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Control Flow in R

bookIntroduction to apply, lapply, and sapply

The apply family of functions in R—apply, lapply, and sapply—offers a powerful and concise way to perform repetitive operations on data structures like matrices, lists, and vectors. These functions are especially useful for data manipulation because they let you avoid writing explicit loops, making your code shorter, more readable, and often faster. Each function in the family is designed for a specific type of data and output, and understanding their differences helps you choose the best tool for your data task.

12345
# Create a matrix mat <- matrix(1:9, nrow = 3) # Use apply to calculate the mean of each row row_means <- apply(mat, 1, mean) print(row_means)
copy

The apply function is used to apply a function to the rows or columns of a matrix or array. Its main arguments are:

  • X: the matrix or array you want to operate on;
  • MARGIN: indicates whether to apply the function over rows (1) or columns (2);
  • FUN: the function to apply.

A typical use case is calculating the mean, sum, or other summary statistics for each row or column in a matrix.

Note
Definition

A higher-order function is a function that takes another function as an argument or returns a function as a result. The apply family functions are higher-order functions because you pass them another function (like mean, sum, or a custom function) to perform on each part of your data.

12345
# Create a list of vectors my_list <- list(a = 1:5, b = 6:8, c = 9:15) # Use lapply to get the length of each element lengths <- lapply(my_list, length) print(lengths)
copy

The lapply function applies a function to each element of a list (or vector) and always returns a list. This makes it very useful when you want to process each element of a list individually, such as computing the length, mean, or another summary for each element.

1234
my_list <- list(a = 1:5, b = 6:8, c = 9:15) # Use sapply to get the length of each element and simplify the result lengths_vec <- sapply(my_list, length) print(lengths_vec)
copy

While lapply always returns a list, sapply tries to simplify the result to the most straightforward data structure possible, such as a vector or matrix. Use lapply when you want to ensure the output is always a list, and use sapply when you want a simplified output if possible. If simplification is not possible, sapply will return a list just like lapply.

1. What is the main advantage of using apply over a for loop?

2. How does lapply differ from sapply?

3. What does the MARGIN argument in apply specify?

question mark

What is the main advantage of using apply over a for loop?

Select the correct answer

question mark

How does lapply differ from sapply?

Select the correct answer

question mark

What does the MARGIN argument in apply specify?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookIntroduction to apply, lapply, and sapply

Stryg for at vise menuen

The apply family of functions in R—apply, lapply, and sapply—offers a powerful and concise way to perform repetitive operations on data structures like matrices, lists, and vectors. These functions are especially useful for data manipulation because they let you avoid writing explicit loops, making your code shorter, more readable, and often faster. Each function in the family is designed for a specific type of data and output, and understanding their differences helps you choose the best tool for your data task.

12345
# Create a matrix mat <- matrix(1:9, nrow = 3) # Use apply to calculate the mean of each row row_means <- apply(mat, 1, mean) print(row_means)
copy

The apply function is used to apply a function to the rows or columns of a matrix or array. Its main arguments are:

  • X: the matrix or array you want to operate on;
  • MARGIN: indicates whether to apply the function over rows (1) or columns (2);
  • FUN: the function to apply.

A typical use case is calculating the mean, sum, or other summary statistics for each row or column in a matrix.

Note
Definition

A higher-order function is a function that takes another function as an argument or returns a function as a result. The apply family functions are higher-order functions because you pass them another function (like mean, sum, or a custom function) to perform on each part of your data.

12345
# Create a list of vectors my_list <- list(a = 1:5, b = 6:8, c = 9:15) # Use lapply to get the length of each element lengths <- lapply(my_list, length) print(lengths)
copy

The lapply function applies a function to each element of a list (or vector) and always returns a list. This makes it very useful when you want to process each element of a list individually, such as computing the length, mean, or another summary for each element.

1234
my_list <- list(a = 1:5, b = 6:8, c = 9:15) # Use sapply to get the length of each element and simplify the result lengths_vec <- sapply(my_list, length) print(lengths_vec)
copy

While lapply always returns a list, sapply tries to simplify the result to the most straightforward data structure possible, such as a vector or matrix. Use lapply when you want to ensure the output is always a list, and use sapply when you want a simplified output if possible. If simplification is not possible, sapply will return a list just like lapply.

1. What is the main advantage of using apply over a for loop?

2. How does lapply differ from sapply?

3. What does the MARGIN argument in apply specify?

question mark

What is the main advantage of using apply over a for loop?

Select the correct answer

question mark

How does lapply differ from sapply?

Select the correct answer

question mark

What does the MARGIN argument in apply specify?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1
some-alt