Skip to content

Difference between loops and vectorization

2 messages · Santosh Srinivas, Dieter Menne

#
Hello R-helpers,

A fundamental question ...I'm trying to understand the differences
between loop and vectorization ... I understand that it should be a
natural choice to use apply / adply when it is needed to perform the
same function across all rows of a data frame.
Any pointers on why this is so? Unable to find the right reading place
on the WWW which explains the concept.

Thanks for your help.
S
#
Santosh Srinivas wrote:
Strange. "loop vectorization" gives a few hundred of hits, that tell you
that loops are not as bad in R (compared to S) in many cases. "apply" is
probably the least important vectorization method in R, but I admit that
some texts make us believe that it is the point where you show your
R-talents.

"adply" is different: it is a rather complex function from package plyr by
Hadley Wickham. This package provides some elegant orthogonal functions
under a unified concept. I found it appealing at the first look, but I am
less frequently using it nowadays because it can be terribly slow.

Try the chapter in 

http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

Dieter