ComenzarEmpieza gratis

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.

Este ejercicio forma parte del curso

Intermediate R for Finance

Ver curso

Instrucciones del ejercicio

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?

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# 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()
___
Editar y ejecutar código