lapply(X, FUN, ...) sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE) simplify2array(x, higher = TRUE)
X | any object; usually a list or vector. Missing values (NAs) are allowed if FUN accepts them. |
FUN | a function to apply to the components of X, or a character string giving the name of the function. |
FUN.VALUE | a prototype value of what FUN returns. The mode and length of the value is used to determine the result. |
... | other arguments to FUN, if any. They can be given with names. |
simplify | a logical flag or a character string. If simplify is TRUE (the default), the result is simplified to an array. If simplify is the character string "array", the result is of the form "higher rank", if possible. |
USE.NAMES | a logical flag. If TRUE (the default), and if X is a character vector, use X as names for the result, unless names already exist. |
x | any object, usually a list, which to be converted to an array. |
higher | a logical flag. If TRUE, the result will be "higher rank" array form if possible. |
lapply | returns a list like X (the same length and names), where each component of the list is replaced by the result of executing FUN on that component. The components of X are passed as the first argument to FUN not matched by one of the ... arguments. |
sapply | returns an object whose first value(s)
is (are) the result of FUN applied to the first component of
X, and so on.
|
vapply | returns an object whose first value(s) is (are)
the result of FUN applied to the first component of X,
and so on. The mode of the object is the same as that of
FUN.VALUE.
|
simplify2array | returns an array, vector or matrix. |
# generate a list with elements 1, 1:2, 1:3, ... lapply(1:5, seq)# generate a list with elements 1, c(2, 2), c(3, 3, 3), ... lapply(1:5, function(x) rep(x,x))
# paste the prefix to the elements of LETTERS # Here X is used as the second argument to the function! lapply(LETTERS, function(pre,post) paste(pre,post), pre="prefix")
# get list with quantiles for each list element lapply(list(a=1:10, b=runif(20)), quantile, probs=1:3/4) # get matrix with quantiles for each list element sapply(list(a=1:10, b=runif(20)), quantile, probs=1:3/4)
# Looping using for(), and equivalent loop using sapply and vapply result <- rep(NA, 100); for(i in 1:100) result[i] <- sum(1:i) result2 <- sapply(1:100, function(i) sum(1:i)) result3 <- vapply(1:100, function(i) sum(1:i), integer(1))
# To operate on two lists let X be a vector of indices list1 <- lapply(1:6, runif) # artificial data for this example list2 <- lapply(1:6, runif) lapply(1:6, function(i, x, y) x[[i]] + y[[i]], x = list1, y = list2)
# Calling lapply() within another function. # The internal function f() uses lexical scoping # to access the variable myConstant addConstant <- function(x, myConstant){ f <- function(x) x+myConstant lapply(x, f) } addConstant(list(a=1:10, b=seq(1,20,length=5)), 100)
# Another way to do this, passing the constant as a ... argument # to lapply, which is then passed to function f() addConstant2 <- function(x, myConstant){ f <- function(x, y) x+y lapply(x, f, myConstant) } addConstant2(list(a=1:10, b=seq(1,20,length=5)), 100)
# Be careful with sapply when the input can have length 0 x <- numeric() sapply(x, sin) # Returns list() vapply(x, sin, numeric(1)) # Returns numeric()
# Generate a matrix with vapply vapply(seq(0,pi,len=10), function(x) c(sine=sin(x), cosine=cos(x)), numeric(2))
# examples for simplify2array
x <- list(a=c(1, 2), b=1) simplify2array(x)
z<-c(1, 2, 3) simplify2array(z)
y <- list(a = c(1, 2), b = c(3, 4)) simplify2array(y)
y.higher <- list(a = matrix(1:2, 1), b = matrix(5:6, 1)) simplify2array(y.higher) simplify2array(y.higher, higher = FALSE)