sapply with function returning vector
In the previous exercises, you've seen how sapply()
simplifies the list that lapply()
would return by turning it into a vector. But what if the function you're applying over a list or a vector returns a vector of length greater than 1? If you don't remember from the video, don't waste more time in the valley of ignorance and head over to the instructions!
This exercise is part of the course
Intermediate R
Exercise instructions
- Finish the definition of the
extremes()
function. It takes a vector of numerical values and returns a vector containing the minimum and maximum values of a given vector, with the names "min" and "max", respectively. - Apply this function over the vector
temp
usingsapply()
. - Finally, apply this function over the vector
temp
usinglapply()
as well.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# temp is already available in the workspace
# Create a function that returns min and max of a vector: extremes
extremes <- function(x) {
c(min = min(x), ___ = ___)
}
# Apply extremes() over temp with sapply()
# Apply extremes() over temp with lapply()