LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Foundations of Functional Programming with purrr

Kurs anzeigen

Anleitung zur Übung

  • 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_.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Create a piped workflow that returns double vectors
___ %>%  
  map___(possibly(function(___){
  # Convert centimeters to feet
    ___ * 0.0328084
}, ___)) 
Code bearbeiten und ausführen