Site icon R-bloggers

Applying Functions To Lists Exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.