Skip to content
Prev 105642 / 398506 Next

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.