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
Anleitung zur Übung
- Pipe the
height_cmobject into amap_*()function that returns double vectors. - Convert each element in
height_cminto feet (multiply it by 0.0328084). - Since not all elements are numeric, use
possibly()to replace instances that do not work withNA_real_.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Create a piped workflow that returns double vectors
___ %>%
map___(possibly(function(___){
# Convert centimeters to feet
___ * 0.0328084
}, ___))