The `lapply()`

function applies a function to individual values of a list, and is a faster alternative to writing loops.

Structure of the `lapply()`

function:

`lapply(LIST, FUNCTION, ...)`

The list variable used for these exercises:

`list1 <- list(observationA = c(1:5, 7:3), observationB=matrix(1:6, nrow=2))`

Answers to the exercises are available here.

**Exercise 1**

Using `lapply()`

, find the length of `list1`

‘s observations.

**Exercise 2**

Using `lapply()`

, find the sums of `list1`

‘s observations.

**Exercise 3**

Use `lapply()`

to find the quantiles of `list1`

.

**Exercise 4**

Find the classes of `list1`

‘s sub-variables, with `lapply()`

.

**Exercise 5**

Required function:

`DerivativeFunction <- function(x) { log10(x) + 1 }`

Apply the “`DerivativeFunction`

” to `list1`

.

**Exercise 6**

Script the “`DerivativeFunction`

” within `lapply()`

. The dataset is `list1`

.

**Exercise 7**

Find the unique values in `list1`

.

**Exercise 8**

Find the range of `list1`

.

**Exercise 9**

Print `list1`

with the `lapply()`

function.

**Exercise 10**

Convert the output of Exercise 9 to a vector, using the `unlist()`

, and `lapply()`

, functions.

**What's next:**

- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

## Leave a Reply