Aan de slagGa gratis aan de slag

sapply with your own function

Like lapply(), sapply() allows you to use self-defined functions and apply them over a vector or a list:

sapply(X, FUN, ...)

Here, FUN can be one of R's built-in functions, but it can also be a function you wrote. This self-written function can be defined before hand, or can be inserted directly as an anonymous function.

Deze oefening maakt deel uit van de cursus

Intermediate R

Cursus bekijken

Oefeninstructies

  • Finish the definition of extremes_avg(): it takes a vector of temperatures and calculates the average of the minimum and maximum temperatures of the vector.
  • Next, use this function inside sapply() to apply it over the vectors inside temp.
  • Use the same function over temp with lapply() and see how the outputs differ.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# temp is already defined in the workspace

# Finish function definition of extremes_avg
extremes_avg <- function(___) {
  ( min(x) + ___ ) / 2
}

# Apply extremes_avg() over temp using sapply()


# Apply extremes_avg() over temp using lapply()
Code bewerken en uitvoeren