How To Use Lapply in R To Apply a Function to a List or Vector

The R programming language is well known for its powerful data manipulation libraries. The base R language provides you with a number of tools that make it easy to manipulate huge data sets within only a few lines of code. And one of the best examples of this powerful capability can be found in the apply family of functions. Each of these functions is what’s known as a vectorized function. These functions can all take a collection of variables and act on all of them at once. The apply family consists of the lapply function, sapply function, tapply function, and mapply function. And you’ll soon see how we can use one of them, lapply, to apply a function to lists or vectors.

What Is Lapply?

Before we start working with lapply we need to define the function and consider what makes it so important. The R programming language provides users with a number of special variables in the form of a vector. Likewise, it has a number of different containers which can hold vectors or even other containers. Each of the containers has special attributes, benefits, and limitations. For example, all elements in an R data frame need to have the same length. While lists are the most flexible container and has very few limitations.

The differing nature of R’s container types means that the system can’t always maintain full compatibility between functions that act on them. For example, a function designed to work with frames can’t be expected to manage the larger variety of forms found in lists. This is where the variants of apply come in. The main apply function is designed around a data frame format, while variants like lapply target other containers. In the case of lapply specifically, we can pair it with R lists. But with that in mind, it’s time to take a more hands-on approach to the use of lapply in R.

The Basics of Lapply

We can begin by considering a simple scenario involving lists. Imagine that we need to find the total value of every item within our lists. The most obvious way of accomplishing this would be to iterate over the elements of the list with a loop. For example, we might try something like the following code.

ourList <- list(first=c(5,10,15),
second=c(20,25,30))

ourFinalValue <- 0
for (x in ourList) {
ourFinalValue = ourFinalValue + x
}
ourFinalValue = sum(ourFinalValue)
print(ourFinalValue)

We begin by creating an R list containing two elements that contain multiples of 5. Next, we declare a new variable called ourFinalValue with a value of 0. We proceed to use a for loop to iterate through the list. With each iteration, we add the value found in our current step through ourList to ourFinalValue. At the end of the loop, we have the totals of the two rows in ourFinalValue. If we wanted to keep these values in list form this would be the final step. But we can also run sum to add the final list values together into a single number and print the result to screen.

As you might imagine, our loop isn’t the most efficient way to handle this problem in R. We’d ideally just use R’s sum function to automatically total every item in a container. This idea is illustrated below.

ourList <- list(first=c(5,10,15),
second=c(20,25,30))
ourVector <- c(first=c(5,10,15),
second=c(20,25,30))

ourFinalValue <- sum(ourVector)
print(ourFinalValue)

This almost gives us everything we need to get a summed value from lists. We begin by defining and populating the list’s contents again. However, this time around we also create a vector containing the same information. We then recreate ourFinalValue and populate it with the returned information from the sum function. That function, in turn, is adding together all of the values in ourVector. And, finally, the last line prints the relevant information to our screen. This might seem to be exactly what we were after. But, it’s not quite that easy. Try changing the ourFinalValue assignment to the following.

ourFinalValue <- sum(ourList)

R’s interpreter will fail with an error telling us that we’re using an invalid type. Our problem stems from the fact that sum isn’t compatible with lists. But this is also where we can see why lapply is such a powerful part of R’s functionality.

Taking Lapply Theory Into Practice

Take a look at the following code to see what using lapply in R can do.

ourList <- list(first=c(5,10,15),
second=c(20,25,30))
ourVector <- c(first=c(5,10,15),
second=c(20,25,30))
ourFinalValue <- lapply(ourList,sum)
print(ourFinalValue)

This code starts out in a manner relatively similar to what we’ve worked with up until this point. We once again create ourList and populate it with the familiar values. We also create a standard vector called ourVector which contains the same information as ourList.

We finally leverage lapply in the next step. When we use lapply we need to supply it with at least two parameters. The initial value is the name of the lists we want to use with lapply. And the second parameter refers to the function that will use information from the first parameter. In this case, we’re using the ourList variable and running it through R’s sum function. The results of this action are assigned to ourFinalValue. And on the last iline we print out the result of this whole process to screen. Note that we have the same total as in the initial example that used a for loop.

You should also take a moment to consider just how concise this code is when compared to the for loop strategy. If we remove the print statement and variable declaration then the entire logic structure can be boiled down to just a single line. What’s more, we can even use this method with vectors. Just change the ourFinalValue assignment into the following.

ourFinalValue <- sum(ourVector)

We could also create a single non-list result using either vectors or lists. Just replace the call to lapply with the following line.

ourFinalValue <- Reduce(“+”,lapply(ourList,sum))

And, once again, this will also work if ourList is replaced with ourVector. In either case, the summed result from lapply will be further reduced into a single value by R’s reduce function.

Need more options? Check Out The articles below…

Scroll to top
Privacy Policy