ComeçarComece de graça

Convert values with possibly()

Let's say you need to convert the Star Wars character heights in sw_people from centimeters to feet. You already know that some of the heights have missing data, so you will use possibly() to convert missing values into NA. Then you will multiply each of the existing values by 0.0328084 to convert them from centimeters into feet.

To get a feel for your data, print out height_cm in the console to check out the heights in centimeters.

Este exercício faz parte do curso

Foundations of Functional Programming with purrr

Ver curso

Instruções do exercício

  • Pipe the height_cm object into a map_*() function that returns double vectors.
  • Convert each element in height_cm into feet (multiply it by 0.0328084).
  • Since not all elements are numeric, use possibly() to replace instances that do not work with NA_real_.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Create a piped workflow that returns double vectors
___ %>%  
  map___(possibly(function(___){
  # Convert centimeters to feet
    ___ * 0.0328084
}, ___)) 
Editar e executar o código