How to Configure R
How to Use the Clipboard to Copy and Paste Data in R
How to Search for Multiple Words 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)
$hours
[1]  25 125
$public
[1]  TRUE FALSE
$type
[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 Do Matrix Arithmetic in R
How to Format and Perform Operations on Dates and Times in R
How to Debug Your Code in R
How to Use Arithmetic Vector Operations in R
How to Add Points to a Plot in R
Advertisement

Inside Dummies.com