Use lapply with a built-in R function
Before you go about solving the exercises below, have a look at the documentation of the lapply()
function. The Usage section shows the following expression:
lapply(X, FUN, ...)
To put it generally, lapply
takes a vector or list X
, and applies the function FUN
to each of its members. If FUN
requires additional arguments, you pass them after you've specified X
and FUN
(...
). The output of lapply()
is a list, the same length as X
, where each element is the result of applying FUN
on the corresponding element of X
.
Now that you are truly brushing up on your data science skills, let's revisit some of the most relevant figures in data science history. We've compiled a vector of famous mathematicians/statisticians and the year they were born. Up to you to extract some information!
This exercise is part of the course
Intermediate R
Exercise instructions
- Have a look at the
strsplit()
calls, that splits the strings inpioneers
on the:
sign. The result,split_math
is a list of 4 character vectors: the first vector element represents the name, the second element the birth year. - Use
lapply()
to convert the character vectors insplit_math
to lowercase letters: applytolower()
on each of the elements insplit_math
. Assign the result, which is a list, to a new variablesplit_low
. - Finally, inspect the contents of
split_low
withstr()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# The vector pioneers has already been created for you
pioneers <- c("GAUSS:1777", "BAYES:1702", "PASCAL:1623", "PEARSON:1857")
# Split names from birth year
split_math <- strsplit(pioneers, split = ":")
# Convert to lowercase strings: split_low
# Take a look at the structure of split_low