Loop Functions

R has some very useful functions which implement looping in a compact form to make the life easier. The very rich and powerful family of apply functions is made of intrinsically vectorized functions. These function in R allow you to apply some function to a series of objects (eg. vectors, matrices, dataframes or files). They include:

  1. lapply(): Loop over a list and evaluate a function on each element
  2. sapply(): Same as lapply but try to simplify the result
  3. apply(): Apply a function over the margins of an array
  4. tapply(): Apply a function over subsets of a vector
  5. mapply(): Multivariate version of lapply

There is another function called split() which is also useful, particularly in conjunction with lapply.

Repeat, Next, Break