map() argument alternatives
You can also use iteration to answer a question, like how long is each element in the wesanderson
dataset. You can do this by feeding map()
a function like length()
. You can do this using the map(list, function)
syntax and it works just fine. However, future exercises get more complex, you will need to learn how to do this second way, using:
map(list, ~function(.x))
This second way gives the same result as map(list, function)
. To specify how the list is used in the function, use the argument .x
to denote where the list element goes inside the function. When you want to use .x
to show where the element goes in the function, you need to put a ~
in front of the function in the second argument of map()
.
This exercise is part of the course
Foundations of Functional Programming with purrr
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Map over wesanderson to get the length of each element
map(___, ___)