[R] Is there a summary on different version of 'apply' functions?What is the meaning of the prefixes?
William Dunlap
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?
>
> Hi,
>
> 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
the
return value being a list.
sapply(X,FUN,...): like lapply(), but also try to 'simplify' the
returned
value. E.g., if all values in the output list are scalar
numerics,
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
simplify.
apply(X,MARGIN,FUN,...): apply FUN to slices of multiway array X
and 'simplify' the results in the manner of sapply. It has no
prefix,
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
that list,
and reformat the result into a k dimensional table of return
values
of FUN. 't' is for the table format of the output.
R has mapply(FUN, x1, x2, ..., xn) where the vectors x1,...,xn are
expanded
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
can choose
whether the result should be 'simplified' or not. The 'm' is for
'multiple
arguments'.
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.
Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com
> Regards,
> Peng
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list