Applying Functions To Lists Exercises

September 19, 2016

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

checklist-1622517_960_720The 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. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)