How to Predict New Data Values with R
How to Use Regular Expressions in R
How to Name the Values in Your Vectors in R

How to Get Lists with the lapply Function in R

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.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Use Logical Vectors as Indices in R
How to Fix Data Reading Errors in R
How to Create a Bar Chart Using ggplot2 in R
How to Summarize a Dataset in R
How to Make Common Graphs with Lattice in R