The lapply, vapply, sapply, and apply functions

These functions are equivalent to a for-each loop with the advantage that they are much more efficient in terms of performance. Basically, the function is applied over every item in a vectorized object. Its main structure is:

function(object_to_iterate_on, function, additional_arguments(separated by commas))

vapply() and apply() have additional arguments that will be covered in detail in the expanded explanations of these functions.

The function argument can be an already defined function (with its arguments), as follows:

sample.list <- list(a=runif(100,0,1), b=runif(500,0,100),
c=runif(35,0,200))
sapply(sample.list, quantile, probs=0.75)
##       a.75%       b.75%       c.75% 
##   0.7145661  77.4817679 158.9351519

Get Learning Shiny 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.