• Post author:
  • Post category:R Programming
  • Post last modified:August 18, 2024
  • Reading time:14 mins read
You are currently viewing Explain lapply() Function in R

R lapply() function is used to apply the function to each element of a list or a vector and return a list of the same length as the input. It is part of the apply family of functions that provide a flexible approach to iteration over data structures. By using lapply(), you can perform operations on complex data structures without the need for explicit loops, leading to cleaner and more efficient code. This function is particularly useful for applying custom functions or built-in functions to elements of a data frame, list, or vector.

Advertisements

In this article, I will explain the lapply() function, including its syntax, parameters, and usage, and demonstrate how to apply this function to each element of a vector or list and each column of a data frame through multiple examples.

Key points-

  • lapply applies a specified function to each element of a list, vector, or data frame and returns a list.
  • Regardless of the input type, lapply always returns a list.
  • Custom functions with multiple arguments can be used with lapply by passing additional arguments through the ... parameter.
  • Using lapply simplifies code by reducing the need for explicit loops, making the code more readable and concise.
  • Anonymous functions can be used directly within lapply for simple operations, avoiding the need to define separate functions.
  • sapply is similar to lapply but attempts to simplify the result to the lowest possible dimension, such as a vector or matrix, if possible.
  • lapply can be nested to handle multi-dimensional data structures, applying functions recursively to elements within lists of lists.
  • When applied to data frames, lapply operates on columns as lists, making it useful for column-wise operations like calculating lengths, means, or applying transformations.
  • Built-in R functions can also be used with lapply(), making it a powerful tool for a wide range of operations.

lapply() Function

The lapply() function iterates over each element of the input list or vector, applies the specified function to each element, and returns the results in a new list. This function is closely related to other apply functions like sapply() and vapply(). However, lapply() always returns a list, while sapply() attempts to simplify the result to a vector or matrix, and vapply() allows for specifying the return type.

Syntax of lapply()

Following is the syntax of the lappy() Function.


# Syntax of lapply() function
lapply(list, FUN, ....)

Parameters

  • X: A vector (atomic or list) or an expression object.
  • FUN: The function to be applied to each element of X.
  • ...: Additional arguments to be passed to FUN.

Return Value

It returns a list of the same length as the input.

Use lapply() Function to R Data Frame

When you use the lapply() function to apply a specified function to a data frame, it will be applied to all elements in the data frame. Let’s create a data frame and pass it to lapply() along with the specified function to obtain a result in the form of a list, with the same length as the input.


# Use lapply() function to data frame
df <- data.frame(Names = c("Ram", "Varun", "Tej"))
print("Given data frame:")
print(df)
print("Get the type of df:")
print(class(df))
result <- lapply(df, nchar)
print("After applying the function to df")
print(result)
print("Get the type of result:")
print(class(result))

Yields below output.

r lapply

Use lapply() Function to R Vector

You can also utilize lapply on a vector to apply a function to each element. Simply pass the vector and the specified function to lapply to obtain the desired result in a list, maintaining the same length as the input vector.


# Use lapply() function to vector
vec <- c("Ram", "Varun", "Tej")
print("Given vector:")
print(vec)
print("Get the type of vec:")
print(class(vec))
result <- lapply(vec, toupper)
print("After applying the function to vector")
print(result)
print("Get the type of result:")
print(class(result))

Yoelds below output.

r lapply

lapply() with Multiple Arguments in R

Alternatively, you can use the lapply() with multiple arguments by defining a custom function that takes multiple parameters. First, define a vector and create a custom function that adds a specific feature to each element. When lapply() is applied to the vector using this function, it iterates over each element, producing a list of modified elements.


lapply() with multiple arguments 
# Define the vector
vec <- c("Ram", "Varun", "Tej")

# Define a custom function that takes multiple arguments
add_suffix <- function(name, suffix) {
  return(paste(name, suffix, sep = "_"))
}

# Define the suffix
suffix <- "2024"

# Use lapply with the custom function and additional arguments
result <- lapply(vec, add_suffix, suffix = suffix)

print("After applying the function to vector:")
print(result)
print("Get the type of result:")
print(class(result))

Yields below output.


# Output:
[1] "After applying the function to vector:"

> print(result)
[[1]]
[1] "Ram_2024"

[[2]]
[1] "Varun_2024"

[[3]]
[1] "Tej_2024"

[1] "Get the type of result:"
[1] "list"

lapply with a Custom Function

You can use the lapply() function with a custom function to perform a specific operation on each element of a vector. First, define a custom function that performs the desired operation on each number, and then apply this function to a vector of numbers using lapply().


# lapply with custom function
# Create the vector of numbers
num_vec <- c(1, 2, 3, 4, 5)

# Create a custom function that calculates the square of a number
square <- function(x) {
  return(x^2)
}

# Use lapply with the custom function
result <- lapply(numbers, square)
print("Given vector:")
print(num_vec)
print("After applying the custom function to vector:")
print(result)
print("Get the type of result:")
print(class(result))

Yields below output.


# Output:
[1] "Given vector:"
[1] 1 2 3 4 5

[1] "After applying the custom function to vector:"
[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25


[1] "Get the type of result:"
[1] "list"

Difference between lapply and For Loop

The lapply() function and for loops are both used for iterating over elements in R, but they have some key differences in terms of usage and efficiency.

lapply() is a functional programming tool that applies a given function to each element of a list or vector and returns a list. Using lapply() often results in more concise and readable code compared to a for loop, particularly for simple operations. It consistently returns a list, regardless of the input type.

Whereas for loop is a more general control flow statement that allows for explicit iteration over elements, providing more flexibility for complex logic.

Let’s create a numeric vector and use both the lapply() function and a for loop to implement a squaring function. We’ll observe the differences between the two approaches.

Using the lapply() function, you can apply a squaring function to each element in a vector, resulting in a list that contains the squares of the numbers in the vector.


# Define the vector of numbers
num_vec <- c(1, 2, 3, 4, 5)

# Use lapply to calculate the square of each number
result <- lapply(numbers, function(x) x^2)
print("After applying function to vector:")
print(result)
print("Get the type of result:")
print(class(result))

Yields below output.


# Output:
[1] "After applying function to vector:"

[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25

[1] "Get the type of result:"
[1] "list"

It provides more concise and readable code compared to a for loop

You can use a for loop to obtain the squares of a given numeric vector. First, create a numeric vector, initialize an empty list to store the squared numbers, and then use a for loop to calculate the square of each number in the vector. This step-by-step process increases the code’s clarity and structure.


# Define the vector of numbers
num_vec <- c(1, 2, 3, 4, 5)

# Create an empty list to store the squared numbers
result <- vector("list", length(num_vec))

# Use a for loop to calculate the square of each number
for (i in seq_along(num_vec)) {
  result[[i]] <- num_vec[i]^2
}

print("Get the result using for loop:")
print(result)
print("Get the type of result:")
print(class(result))

Yields below output.


# Output:
[1] "Get the result using for loop:"

[[1]]
[1] 1

[[2]]
[1] 4

[[3]]
[1] 9

[[4]]
[1] 16

[[5]]
[1] 25

[1] "Get the type of result:"
[1] "list"

Difference between lapply and sapply

Both lapply and sapply are used to apply a function to each element of a list or vector in R. However, they differ in their return types.

lapply applies a function to each element of a list or vector and always returns a list, regardless of the original input’s type.

sapply is a user-friendly version of lapply. It attempts to simplify the result into a vector or matrix if possible. If the result can’t be simplified, it returns a list.

Let’s observe the return values when we apply both methods to each element of a numeric vector and compare their outputs.


# Define the vector of numbers
num_vec <- c(1, 2, 3, 4, 5)

# Use lapply to calculate the square of each number
result <- sapply(numbers, function(x) x^2)
print("After applying function to vector:")
print(result)
print("Get the type of result:")
print(class(result))

Yields below output.


# Output:
[1] "After applying function to vector:"
[1]  1  4  9 16 25

[1] "Get the type of result:"
[1] "numeric"

Nested lapply Function

Using nested lapply functions can be particularly useful when dealing with multi-dimensional data structures, such as lists of lists.

Let’s create a list of lists, where each sublist contains numbers, and then use nested lapply to square each number within each sublist.


# Define a list of lists
nested_list <- list(
  list(1, 2, 3),
  list(4, 5, 6)
)
print("Original nested list:")
print(nested_list)

# Use nested lapply to calculate the square of each number in each sublist
result <- lapply(nested_list, function(inner_list) {
  lapply(inner_list, function(x) x^2)
})
print("After applying nested lapply function to nested list:")
print(result)

Yields below output.


# Output:
[1] "After applying nested lapply function to nested list:"
[[1]]
[[1]][[1]]
[1] 1

[[1]][[2]]
[1] 4

[[1]][[3]]
[1] 9


[[2]]
[[2]][[1]]
[1] 16

[[2]][[2]]
[1] 25

[[2]][[3]]
[1] 36

Conclusion

In this article, I have explained that the lapply() function is essential in R for functional programming. It provides a versatile and efficient way to perform iterative operations on various data structures. Its ability to simplify code and handle complex transformations makes it a valuable tool for data manipulation and analysis.

Happy learning!!