Applying Functions to Lists
Two functions are handy for applying functions to lists: lapply
and sapply
.
Using the lapply() and sapply() Functions
The function lapply()
(for list apply) works like the matrix apply()
function, calling the specified function on each component of a list (or vector coerced to a list) and returning another list. Here’s an example:
> lapply(list(1:3,25:29),median) [[1]] [1] 2 [[2]] [1] 27
R applied median()
to 1:3 and to 25:29, returning a list consisting of 2 and 27.
In some cases, such as the example here, the list returned by lapply()
could be simplified to a vector or matrix. This is exactly what sapply()
(for simplified [l]apply) does.
> sapply(list(1:3,25:29),median) [1] 2 27
You saw an example of matrix output in ...
Get The Art of R Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.