Use lapply with your own function
As Filip explained in the instructional video, you can use lapply()
on your own functions as well. You just need to code a new function and make sure it is available in the workspace. After that, you can use the function inside lapply()
just as you did with base R functions.
In the previous exercise you already used lapply()
once to convert the information about your favorite pioneering statisticians to a list of vectors composed of two character strings. Let's write some code to select the names and the birth years separately.
The sample code already includes code that defined select_first()
, that takes a vector as input and returns the first element of this vector.
This exercise is part of the course
Intermediate R
Exercise instructions
- Apply
select_first()
over the elements ofsplit_low
withlapply()
and assign the result to a new variablenames
. - Next, write a function
select_second()
that does the exact same thing for the second element of an inputted vector. - Finally, apply the
select_second()
function oversplit_low
and assign the output to the variableyears
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Code from previous exercise:
pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
split <- strsplit(pioneers, split = ":")
split_low <- lapply(split, tolower)
# Write function select_first()
select_first <- function(x) {
x[1]
}
# Apply select_first() over split_low: names
# Write function select_second()
# Apply select_second() over split_low: years