← Back to R Course | Chapter 11: Apply Family | Lesson 2 of 6

lapply()

In this page:

  1. lapply()

lapply()

lapply(x, FUN) applies a function to every element of a vector or list and always returns a list, regardless of what the function itself returns. It's the most general member of the apply family, since a list result can hold values of any type or length. Use lapply() when you don't need the result simplified to a vector.

Note: lapply() always returns a list -- use sapply() instead when you want the result automatically simplified to a vector.

Example: lapply()

markup
numbers <- list(1, 2, 3, 4)

squared <- lapply(numbers, function(x) x^2)
print(squared)
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.