How to Get Lists with the lapply Function in R

By Andrie de Vries, Joris Meys

The lapply() function works exactly the same as the sapply() function, with one important difference: It always returns a list. This trait can be beneficial if you’re not sure what the outcome of sapply() will be.

Say you want to know the unique values of only a subset of the data frame clients. You can get the unique values in the first and third rows of the data frame like this:

> sapply(clients[c(1,3), ], unique)
     hours public  type
[1,] "25"  "TRUE"  "public"
[2,] "125" "FALSE" "private"

But because every variable now has two unique values, sapply() simplifies the result to a matrix. If you counted on the result to be a list in the following code, you would get errors. If you used lapply(), on the other hand, you would also get a list in this case, as shown in the following output:

> lapply(clients[c(1,3), ], unique)
[1]  25 125
[1] "public"  "private"

Actually, the sapply() function has an extra argument, simplify, that you can set to FALSE if you don’t want a simplified list. If you set both the arguments simplify and USE.NAMES to FALSE, sapply() and lapply() return exactly the same result.