lapply(base)
lapply()所属R语言包:base
Apply a Function over a List or Vector
套用在一个列表或向量的函数
译者:生物统计家园网 机器人LoveR
描述----------Description----------
lapply returns a list of the same length as X, each element of which is the result of applying FUN to the corresponding element of X.
lapply返回一个列表长度相同X,其中每个元素的应用FUNX相应的元素的结果。
sapply is a user-friendly version and wrapper of lapply by default returning a vector, matrix or, if simplify="array", an array if appropriate, by applying simplify2array(). sapply(x, f, simplify=FALSE, USE.NAMES=FALSE) is the same as lapply(x,f).
sapply是一个用户友好的版本,并返回一个向量,矩阵的默认lapply包装,或如果simplify="array",一个数组,如果适当的申请simplify2array()。 sapply(x, f, simplify=FALSE, USE.NAMES=FALSE)是lapply(x,f)相同。
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是类似sapply,但有一个预先指定的返回值的类型,因此它可以使用更安全(有时更快)。
replicate is a wrapper for the common use of sapply for repeated evaluation of an expression (which will usually involve random number generation).
replicate是sapply重复评价一个表达式(通常会涉及随机数生成)共同使用的包装。
simplify2array() is the utility called from sapply() when simplify is not false and is similarly called from mapply().
simplify2array()是从实用称为sapply()当simplify是不是假的,同样呼吁从mapply()。
用法----------Usage----------
lapply(X, FUN, ...)
sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)
replicate(n, expr, simplify = "array")
simplify2array(x, higher=TRUE)
参数----------Arguments----------
参数:X
a vector (atomic or list) or an expression object. Other objects (including classed objects) will be coerced by base::as.list.
矢量(原子或列表)或expression对象的。其他对象(包括类对象)将被迫base::as.list。
参数:FUN
the function to be applied to each element of X: see "Details". In the case of functions like +, %*%, the function name must be backquoted or quoted.
功能被应用到每个元素的X:看到“详细资料”。在像+,%*%,函数的名称必须backquoted或引述的职能。
参数:...
optional arguments to FUN.
FUN可选参数。
参数:simplify
logical or character string; should the result be simplified to a vector, matrix or higher dimensional array if possible? For sapply it must be named and not abbreviated. The default value, TRUE, returns a vector or matrix if appropriate, whereas if simplify = "array" the result may be an array of “rank” (=length(dim(.))) one higher than the result of FUN(X[[i]]).
逻辑或字符串的结果应该被简化为一个向量,矩阵或更高维数组如果可能的话吗? sapply它必须被命名为不缩写。默认值,TRUE,返回一个向量或矩阵,如果合适,而如果simplify = "array"结果可能是array“排名”(= length(dim(.)) )一高比FUN(X[[i]])的结果。
参数: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.
逻辑;如果TRUE和X字符,请使用Xnames的结果,除非它有名字已经。由于这一论点如下...它的名字不能缩写。
参数:FUN.VALUE
a (generalized) vector; a template for the return value from FUN. See "Details".
(广义)的向量;一个有趣的返回值的模板。见“详细资料”。
参数:n
integer: the number of replications.
整数的重复数量。
参数:expr
the expression (language object, usually a call) to evaluate repeatedly.
多次表达(语言的对象,通常是调用)来评估。
参数:x
a list, typically returned from lapply().
一个列表,通常从lapply()返回。
参数:higher
logical; if true, simplify2array() will produce a (“higher rank”) array when appropriate, whereas higher = FALSE would return a matrix (or vector) only. These two cases correspond to sapply(*, simplify = "array") or simplify = TRUE, respectively.
逻辑,如果属实,simplify2array()将产生(“高排名”)阵列在适当的时候,而higher = FALSE将只返回一个矩阵(或向量)。这两种情况下sapply(*, simplify = "array")或simplify = TRUE,分别对应。
Details
详情----------Details----------
FUN is found by a call to match.fun and typically is specified as a function or a symbol (e.g. a backquoted name) or a character string specifying a function to be searched for from the environment of the call to lapply.
FUN被发现调用match.fun由通常是一个函数或符号(如backquoted名称)或一个字符串,指定要搜索从调用环境的功能指定lapply。
Function FUN must be able to accept as input any of the elements of X. If the latter is an atomic vector, FUN will always be passed a length-one vector of the same type as X.
功能FUN必须能够接受作为输入任何X元素。如果是后者的一个原子的向量,FUN总是会被传递一个相同类型的长度为X一个向量。
Arguments in ... cannot have the same name as any of the other arguments, and care may be needed to avoid partial matching to FUN. In general-purpose code it is good practice to name the first two arguments X and FUN if ... is passed through: this both avoids partial matching to FUN and ensures that a sensible error message is given if arguments named X or FUN are passed through ....
...参数不能有任何其它参数相同的名称,并可能需要照顾,以避免部分匹配FUN。在通用的代码,它是很好的做法,命名前两个参数X和FUN如果...通过这既避免了部分匹配FUN,确保如果参数名为X或FUN...的通过是一个明智的错误消息。
Simplification in sapply is only attempted if X has length greater than zero and if the return values from all elements of X are all of the same (positive) length. If the common length is one the result is a vector, and if greater than one is a matrix with a column corresponding to each element of X.
只试图在的sapply简单化如果X长度大于零,如果所有元素的返回值X都是相同长度(正)。如果共同的长度是一个结果是一个向量,如果大于一个是与相应的每个X元素列矩阵。
Simplification is always done in vapply. 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 < real < complex, but not demoted.)
简化是经常做vapply。此功能检查值FUN兼容FUN.VALUE,因为它们必须有相同的长度和类型。 (类型,可在订货逻辑推动到一个更高的类型<整数<真正的<复杂,但不降级。)
Users of S4 classes should pass a list to lapply and vapply: the internal coercion is done by the as.list in the base namespace and not one defined by a user (e.g. by setting S4 methods on the base function).
S4的类的用户应该通过一个列表lapply和vapply:内部强制进行as.list在基地命名空间,而不是一个定义用户(例如通过设置S4方法基函数)。
lapply and vapply are primitive functions.
lapply和vapply是原始的功能。
值----------Value----------
For lapply, sapply(simplify = FALSE) and replicate(simplify = FALSE), a list.
对于lapply,sapply(simplify = FALSE)和replicate(simplify = FALSE),一个列表。
For sapply(simplify = TRUE) and replicate(simplify = TRUE): if X has length zero or n = 0, an empty list. Otherwise an atomic vector or matrix or list of the same length as X (of length n for replicate). If simplification occurs, the output type is determined from the highest type of the return values in the hierarchy NULL < raw < logical < integer < real < complex < character < list < expression, after coercion of pairlists to lists.
sapply(simplify = TRUE)和replicate(simplify = TRUE):如果X长度为零或n = 0,一个空列表。否则一个原子的向量或矩阵或列表的长度相同X(长度nreplicate)。如果出现简化,确定输出类型,从最高的返回值的类型层次结构中的NULL <原料<逻辑<整数<真正复杂的<字符<列表<表达,胁迫pairlists后到列表。
vapply returns a vector or array of type matching the FUN.VALUE. If length(FUN.VALUE) == 1 a vector of the same length as X is returned, otherwise an array. If FUN.VALUE is not an array, the result is a matrix with length(FUN.VALUE) rows and length(X) columns, otherwise an array a with dim(a) == c(dim(FUN.VALUE), length(X)).
vapply返回一个类型匹配FUN.VALUE的向量或数组。如果length(FUN.VALUE) == 1X返回一个相同长度的向量,否则一个数组。如果FUN.VALUE不array,结果是一个矩阵length(FUN.VALUE)行length(X)列,否则数组a与dim(a) == c(dim(FUN.VALUE), length(X))。
The (Dim)names of the array value are taken from the FUN.VALUE if it is named, otherwise from the result of the first function call. Column names of the matrix or more generally the names of the last dimension of the array value or names of the vector value are set from X as in sapply.
数组值(DIM)的名称是从FUN.VALUE,如果它被命名为,否则从第一个函数调用的结果。从Xsapply矩阵或更普遍的最后一维数组中的值或矢量值的名称的名称列名。
注意----------Note----------
sapply(*, simplify = FALSE, USE.NAMES = FALSE) is equivalent to lapply(*).
sapply(*, simplify = FALSE, USE.NAMES = FALSE)相当于lapply(*)的。
For historical reasons, the calls created by lapply are unevaluated, and code has been written (e.g. bquote) that relies on this. This means that the recorded call is always of the form FUN(X[[0L]], ...), with 0L replaced by the current integer index. This is not normally a problem, but it can be if FUN uses sys.call or match.call or if it is a primitive function that makes use of the call. This means that it is often safer to call primitive functions with a wrapper, so that e.g. lapply(ll, function(x) is.numeric(x)) is required in R 2.7.1 to ensure that method dispatch for is.numeric occurs correctly.
由于历史原因的呼声,lapply创建是不计算,代码已被写入(例如bquote),这依赖。这意味着,记录通话始终是形式FUN(X[[0L]], ...)与0L取代由目前的整数索引,。这是不正常的问题,但它可以是如果FUN使用sys.call或match.call或如果它是一种原始的功能,使通话使用。这意味着,它常常是安全调用一个包装原始功能,因此,如lapply(ll, function(x) is.numeric(x))需要在R 2.7.1以确保is.numeric正确发生的方法调度。
If expr is a function call, be aware of assumptions about where it is evaluated, and in particular what ... might refer to. You can pass additional named arguments to a function call as additional named arguments to replicate: see "Examples".
expr如果是一个函数调用,知道它是评估的假设,特别是什么...可能参考。您可以通过额外的命名参数调用函数额外的命名参数replicate:看到“范例”。
参考文献----------References----------
The New S Language. Wadsworth & Brooks/Cole.
参见----------See Also----------
apply, tapply, mapply for applying a function to multiple arguments, and rapply for a recursive version of lapply(), eapply for applying a function to each entry in an environment.
apply,tapply,mapplyrapply为多个参数,应用功能和递归版本的lapply(),eapply套用函数每个environment的条目。
举例----------Examples----------
require(stats); require(graphics)
x <- list(a = 1:10, beta = exp(-3:3), logic = c(TRUE,FALSE,FALSE,TRUE))
# compute the list mean for each list element[计算列表中的意思为每个列表元素]
lapply(x,mean)
# median and quartiles for each list element[每个列表元素的中位数和四分位数]
lapply(x, quantile, probs = 1:3/4)
sapply(x, quantile)
i39 <- sapply(3:9, seq) # list of vectors[列表向量]
sapply(i39, fivenum)
vapply(i39, fivenum,
c(Min. = 0, "1st Qu." = 0, Median = 0, "3rd Qu." = 0, Max. = 0))
## sapply(*, "array") -- artificial example[#sapply(*,“阵列”) - 人工例如]
(v <- structure(10*(5:8), names=LETTERS[1:4]))
f2 <- function(x,y) outer(rep(x, length.out=3), y)
(a2 <- sapply(v, f2, y = 2*(1:5), simplify="array"))
a.2 <- vapply(v, f2, outer(1:3, 1:5), y = 2*(1:5))
stopifnot(dim(a2) == c(3,5,4), all.equal(a2, a.2),
identical(dimnames(a2), list(NULL,NULL,LETTERS[1:4])))
hist(replicate(100, mean(rexp(10))))
## use of replicate() with parameters:[#使用复制()的参数:]
foo <- function(x=1, y=2) c(x,y)
# does not work: bar <- function(n, ...) replicate(n, foo(...))[不工作:条形< - (N,...)函数复制(N,美孚(...))]
bar <- function(n, x) replicate(n, foo(x=x))
bar(5, x=3)
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|