Skip to content
Prev 105655 / 398506 Next

Usage of apply

But do note -- again! -- that the apply family of functions do their magic
**internally through looping**, so that they are generally not much faster
-- and sometimes a bit slower -- then explicit loops. Their chief advantage
(IMO, of course) is in code clarity and correctness, which is why I prefer
them. (They are also written to do their looping as efficiently as possible,
which explicit looping in user code may not.)

Of course, vectorized calculations (colMeans() in the example below) **are**
much faster and usually clearer than explicit loops.
 
Bert Gunter
Genentech Nonclinical Statistics
South San Francisco, CA 94404

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Chuck Cleland
Sent: Wednesday, December 06, 2006 6:54 AM
To: R Help
Subject: Re: [R] Usage of apply
Jin Shusong wrote:
If you have read the help pages for each and possibly even consulted
the reference on those help pages, you may need to elaborate on what
parts of these functions you don't understand.  You might also describe
a loop you are contemplating and ask how it might be replaced by one of
these functions.
  Here is a very simple example of a loop that could be avoided with one
of these functions:
[1] 5.843333
[1] 3.057333
[1] 3.758
[1] 1.199333

  Here is how you would do that with apply():
Sepal.Length  Sepal.Width Petal.Length  Petal.Width
    5.843333     3.057333     3.758000     1.199333

  Even better in this particular case would be:
Sepal.Length  Sepal.Width Petal.Length  Petal.Width
    5.843333     3.057333     3.758000     1.199333

  but you don't always want mean() or sum() as the function, so the
functions you mention above are more general than colMeans() and similar
functions.
http://www.R-project.org/posting-guide.html