Apply a Function to Components of a List

DESCRIPTION:
Returns a list which is the result of a function that is applied to each element of a list.

USAGE:
lapply(X, FUN, ...)

REQUIRED ARGUMENTS:
X:
a list.
FUN:
function (or character string giving the name of the function) to be applied to the specified array sections. The character form is necessary only for functions with unusual names, e.g., "/".

OPTIONAL ARGUMENTS:
...:
any arguments to FUN; they are passed unchanged.

VALUE:
a list like X where each component of the list has been replaced by the result of executing FUN on that component. The components of X are considered the first argument to FUN that is not matched by any of the ... arguments.

NOTE:
apply applies a function to specified subsets of an array. sapply also applies a function to a list but returns a vector, a matrix, or a list depending on the situation. tapply is useful for ragged arrays (categories).

BUGS:
When FUN is a generic function, f, that calls .Internal instead of UseMethod (e.g., dim, +, or as.vector) lapply will not find the correct method for it (it will use the default method). In those cases it is best to replace FUN=f by FUN=function(x,...)f(x,...). For example, replace FUN="+" by FUN=function(e1,e2)e1+e2 and replace FUN=dim by FUN=function(x)dim(x). This applies to any function that takes another function as an argument, not just to lapply.

SEE ALSO:
sapply , apply , tapply .

EXAMPLES:
#apply mean function with trim argument to each element of list x
lapply(x, mean, trim=.1)

# apply mean to the stack.loss data with various values of trim lapply(list(0, .05, .1, .25, .5), mean, x=stack.loss)

l <- as.list(1:5)

# generate a list with elements 1, 1:2, 1:3, ... lapply(l,seq)

#generate a list with elements 1, c(2, 2), c(3, 3, 3), ... lapply(l, function(x)rep(x,x))