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.
This exercise is part of the course
Foundations of Functional Programming with purrr
Exercise instructions
- Pipe the
height_cm
object into amap_*()
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 withNA_real_
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a piped workflow that returns double vectors
___ %>%
map___(possibly(function(___){
# Convert centimeters to feet
___ * 0.0328084
}, ___))