1. Learn
  2. /
  3. Courses
  4. /
  5. Intermediate R

Connected

Exercise

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.

Instructions

100 XP
  • Apply select_first() over the elements of split_low with lapply() and assign the result to a new variable names.
  • 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 over split_low and assign the output to the variable years.