apply家族函数
对于向量、矩阵、数组、列表、数据框等数据结构,apply家族函数允许用户在各组件上调用指定的函数。
apply家族函数包含apply(),lapply(),sapply(),vapply(),mapply(),tapply()等函数,可以根据需要处理的数据结构和期望输出的数据结构来选择不同的函数
调用的函数允许是内置函数或自定义函数
apply家族函数并不能使程序的运行速度加快。其优点是使程序更紧凑,更易于阅读和修改,并且避免产生使用循环语句时可能带来的bug。 此外,并行运算是R目前发展的方向之一,apply家族函数会变得越来越重要。(R语言编程艺术)
函数说明
apply()函数
godfather of apply family,Apply Functions Over Array Margins;
适用对象:矩阵或数组; 返回值:向量、数组或列表;
用法:
apply(X, MARGIN, FUN, ...)
参数:
- X:数组,包含矩阵
- MRGIN:向量,指出函数将应用于哪些组件;
- 对于矩阵来说,MARGIN = 1,应用于行;MARGIN = 2,应用于列;MARGIN = c(1,2),应用于行与列;
- 如果X的维度有名称,可以使用名称代替Where X has named dimnames, it can be a character vector selecting dimension names.
- FUN:函数名称
- ...:FUN的可选参数集
返回值:
- 如果对于每个组件调用函数的返回值长度相等,则返回一个数组或向量;
- 如果对于每个组件调用函数的返回值长度不相等,返回一个列表;
lapply()函数
list apply, Apply a Function over a List or Vector
适用对象:列表或强制转换成列表的向量、数据框; 返回值:另一个列表;
用法:
lapply(X, FUN, ...)
参数:
- X:列表,或强制转换为列表的向量; a vector (atomic or list) or an expression object. Other objects (including classed objects) will be coerced by base::as.list.
- FUN:函数名称;
- ...:FUN的可选参数集;
返回值: 与X长度相等的列表,列表中的每一项是X中对应项调用FUN的结果;
与apply函数的区别:
- 适用对象不同:
- lapply用于向量、列表、数据框;
- apply用于数组或矩阵
- 返回值类型不同:
- lappy返回值为一个列表,长度与之前的列表相同;
- apply根据结果返回向量、数组或列表
sapply()函数
simplify lapply
- sapply()函数简化了lapply()函数的使用,是lapply()的包装函数。
- 适用对象:列表或强制转换成列表的向量、数据框;
- 返回值:默认返回一个向量、矩阵,或者数组(需要参数设置为
simplify = "array"
)
用法:
sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
参数:
- X:列表,或强制转换为列表的向量; a vector (atomic or list) or an expression object. Other objects (including classed objects) will be coerced by base::as.list.
- FUN:函数名称;
- ...:FUN的可选参数集;
- simplify:
- 布尔值或者字符串,表示返回值是否转换为向量、矩阵或者更高维的数组(前提是这种转换是可行的);
- simplify参数名必须写出并且不能使用简写;
- simplify默认值为TRUE,返回一个向量或者矩阵,如果
simplify ="array"
,则返回数组;
- USE.NAMES:logical; if TRUE and if X is character, use X as names for the result unless it had names already. Since this argument follows ... its name cannot be abbreviated.
mapply()函数
Apply a Function to Multiple List or Vector Arguments
- mapply()函数是sapply的多元形式;
- mapply applies FUN to the first elements of each ... argument, the second elements, the third elements, and so on. Arguments are recycled if necessary.
- Its purpose is to be able to vectorize arguments to a function that is not usually accepting vectors as arguments.
- In short, mapply() applies a Function to Multiple List or multiple Vector Arguments.
用法:
mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE)
参数:
- FUN:function to apply, found via match.fun.
- ...: arguments to vectorize over (vectors or lists of strictly positive length, or all of zero length). See also ‘Details’. MoreArgs: a list of other arguments to FUN.
- SIMPLIFY: logical or character string; attempt to reduce the result to a vector, matrix or higher dimensional array; see the simplify argumentof sapply.
- USE.NAMES:logical; use names if the first ... argument has names, or if it is a character vector, use that character vector as the names.
vapply()函数
类似sapply,更安全、快速
- vapply is similar to sapply, but has a pre-specified type of return value, so it can be safer (and sometimes faster) to use.
用法:
vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)
参数:
- FUN.VALUE:(generalized) vector; a template for the return value from FUN.
This function checks that all values of FUN are compatible with the FUN.VALUE, in that they must have the same length and type. (Types may be promoted to a higher type within the ordering logical < integer < double < complex, but not demoted.)