Use vapply
Before you get your hands dirty with the third and last apply function that you'll learn about in this intermediate R course, let's take a look at its syntax. The function is called vapply()
, and it has the following syntax:
vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)
Over the elements inside X
, the function FUN
is applied. The FUN.VALUE
argument expects a template for the return argument of this function FUN
. USE.NAMES
is TRUE
by default; in this case vapply()
tries to generate a named array, if possible.
For the next set of exercises, you'll be working on the temp
list again, that contains 7 numerical vectors of length 5. We also coded a function basics()
that takes a vector, and returns a named vector of length 3, containing the minimum, mean and maximum value of the vector respectively.
This exercise is part of the course
Intermediate R
Exercise instructions
- Apply the function
basics()
over the list of temperatures,temp
, usingvapply()
. This time, you can usenumeric(3)
to specify theFUN.VALUE
argument.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# temp is already available in the workspace
# Definition of basics()
basics <- function(x) {
c(min = min(x), mean = mean(x), max = max(x))
}
# Apply basics() over temp using vapply()