1. lapply
When you're using R, you will often encounter vectors and lists containing all sorts of information. You've seen before that the for loop is there to help us iterate over all kinds of data structures. But I'm glad to tell you there's an even easier way!
2. NYC: for
Let's have a look at some information related to New York City. It is stored in a list.
Suppose we want to find out the class of each element of this list. As you already know, you could use a for loop that iterates over the different elements of nyc, and call the class() function.
As expected, the first element is numeric, the second one is a character string, and the third is a logical. This however is quite a bit of code just to find out the class of elements of a list, isn't it?
3. NYC: lapply()
lapply is here to rescue us from writing too much code for simple tasks such as these. I don't want to overwhelm you with a complex definition of lapply, so let us simply rewrite our previous statement. Let's replace the for loop with an lapply and see what happens.
Under the hood, lapply iterated over the inputted list, nyc, and on each element of the list, applied the function class(). The output of lapply includes the results of calling the class function over each of the list elements. The first element in the output now contains the class of the first element in nyc, namely "numeric".
4. NYC: lapply()
The second element in the output contains the class of the second element in nyc, "character", and the third element contains logical, the class of the third input element. Also notice that the names of the list, pop, boroughs and capital are maintained here, that's pretty useful!
5. Cities: for
Let's have a look at another example, now with a vector of city names.
Suppose we want to build a vector of the same length as cities, containing the number of characters of each city name. We can do that by using the following for loop.
First, we create an empty vector, called num_chars. Inside the for loop, using a looping index, we gradually fill this vector up using the nchar function. We get the wanted result, but it's a lot of code to do a simple thing, in my opinion.
6. Cities: lapply()
Fortunately, refactoring this code to use lapply is easy. As the input, we use the cities vector as well as the function we want to apply on each element in this vector:
We get a list containing the number of characters of the corresponding cities in the input vector. Notice here that the output is a list, although the input was a vector. This is something important about the lapply function: it always returns a list, irrespective of the input data structure.
7. Cities: lapply()
If you want to convert this list to a vector, you can simply wrap this lapply function inside the unlist() function which turns a list into a vector.
In a single one-liner, we have achieved the exact same thing as the bulky for loop. Moreover, by using lapply our code looks much more intuitive and readable.
8. Oil
In the previous examples we applied the functions class() and nchar() on every element of the input vector or list, but lapply can also be used with functions that you've written yourself. Let's try to write some code to illustrate this. Assume we have a list of oil prices per gallon. We will write a function that triples every element of oil_prices.
We can write the function triple that simply calculates the triple of its input:
Now, all we need to do is applying this triple function on each element of oil_prices using lapply.
Works like a charm! Again, you can unlist the result, if you want your output as a vector rather than as a list:
9. Oil
Now let's try to make this triple function a bit more generic, and instead call it multiply, and have it use an additional argument, factor:
We can now choose with which factor we want to multiply our input. Calling triple is now the same as calling multiply with the factor argument equal to three. But wait, what if we want to use multiply inside the lapply function? How can we specify this additional argument? Well, lapply allows you to add additional arguments to the function: just include them right after the function you want to apply to your list or vector:
Now, on every element of oil_prices, the function multiply is called, with x equal to each element of the input list, and a factor of three every time. See what happens with the oil prices when we change the factor of the multiply function: they all get multiplied by four now.
10. Let's practice!
In the next set of exercises, you'll get hands on experience with the lapply function. You'll learn how to use lapply on built-in functions, your own functions as well as anonymous functions. Have fun!