Get startedGet started for free

Failing to simplify

For interactive use, sapply() is great. It guesses the output type so that it can simplify, and normally that is fine. However, sapply() is not a safe option to be used when writing functions. If sapply() cannot simplify your output, then it will default to returning a list just like lapply(). This can be dangerous and break custom functions if you wrote them expecting sapply() to return a simplified vector.

Let's look at an exercise using a list containing information about the stock market crash of 2008.

This exercise is part of the course

Intermediate R for Finance

View Course

Exercise instructions

The list market_crash has been created for you.

  • Use sapply() to get the class() of each element in market_crash.


A new list, market_crash2 has been created. The difference is in the creation of the date!

  • Use lapply() to get the class() of each element in market_crash2.
  • Use sapply() to get the class() of each element in market_crash2.


date in market_crash2 has multiple classes. Why couldn't sapply() simplify this?

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Market crash with as.Date()
market_crash <- list(dow_jones_drop = 777.68, 
                     date = as.Date("2008-09-28"))
                     
# Find the classes with sapply()
___

# Market crash with as.POSIXct()
market_crash2 <- list(dow_jones_drop = 777.68, 
                      date = as.POSIXct("2008-09-28"))

# Find the classes with lapply()
___

# Find the classes with sapply()
___
Edit and Run Code