[R] Is there a summary on different version of 'apply' functions?What is the meaning of the prefixes?
wdunlap at tibco.com
Wed Aug 12 00:18:25 CEST 2009
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Peng Yu
> Sent: Tuesday, August 11, 2009 1:09 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Is there a summary on different version of
> 'apply' functions?What is the meaning of the prefixes?
> There are quiet a few different 'apply' functions, such as lapply,
> sapply and many more. I'm very familiar with the 'Apply' function in
> Mathematica. Can somebody point me a summary of all the 'apply'
> functions in R. Also, I'm curious that what 'l' and 's' (and other
> prefixes) stand for in 'lapply' and 'sapply'
The 'traditional' apply functions (from S) are
lapply(X,FUN,...): apply FUN to each element of a vector X and return
the results results as a list of the returned values. 'l' is for
return value being a list.
sapply(X,FUN,...): like lapply(), but also try to 'simplify' the
value. E.g., if all values in the output list are scalar
collapse the result to a numeric vector or if all are numerics of
the same length, collapse it to a matrix. The 's' is for
apply(X,MARGIN,FUN,...): apply FUN to slices of multiway array X
and 'simplify' the results in the manner of sapply. It has no
probably because it was the first one written.
tapply(X,INDICES,FUN,...): INDICES is a list of k vectors, each the
length of X. Compute the list split(X,INDICES), call lapply on
and reformat the result into a k dimensional table of return
of FUN. 't' is for the table format of the output.
R has mapply(FUN, x1, x2, ..., xn) where the vectors x1,...,xn are
to the length of the longest and FUN is a function of n variables. It
calls FUN(x1[[i]],x2[[i]],...,xn[[i]]) for i in 1:length(longestX). You
whether the result should be 'simplified' or not. The 'm' is for
R has eapply(env, FUN, ...), which applies FUN to every object in the
environment env. 'e' is for the input 'environment'.
R and Splus have rapply, which calls FUN on every element of the list
passed to it, and if the element is also a list recursively descend into
it. The 'r' is for for the 'recursive' nature of the application.
Note that the letters before 'apply' sometimes refer to the nature of
the output, sometimes to the nature of the input, sometimes to
the nature of the the subsetting, and sometimes to history.
There are lots of specialized *apply functions in the packages.
TIBCO Software Inc - Spotfire Division
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help