Aan de slagGa gratis aan de slag

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!

Deze oefening maakt deel uit van de cursus

Intermediate R

Cursus bekijken

Oefeninstructies

  • 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 using sapply().
  • Finally, apply this function over the vector temp using lapply() as well.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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()
Code bewerken en uitvoeren