[R] vector angle

Laurent Gautier laurent at cbs.dtu.dk
Tue Jul 17 11:29:30 CEST 2001


Prof Brian Ripley wrote:

> On Tue, 17 Jul 2001, Laurent Gautier wrote:
>
> > Evan Zane Macosko wrote:
> >
> > > Hi everyone,
> > >
> > > I'm translating into R some programs I worked through in Matlab to
> > > calculate the angle between two vectors (very large--like 6200 rows in
> > > each vector).  In Matlab, I used a series of nested for loops, because I
> > > was calculating the angles between many pairs of vectors.  I know for
> > > loops are not desirable in R code, so I was wondering if anyone could
> > > recommend a faster way to complete this task.  Also, I have NAs in my
> > > vectors--I've had trouble performing various operations on my vectors in R
> > > because of these NAs.
> > >
> > > Any advice on this would be greatly appreciated.
> >
> > As far as I know, the use of apply (sapply and lapply) would make things run
> > faster than 'for' loops.
>
> Not very much faster in R (and apply itself is basically a for loop).
> Because for loops were slow in S3, the message seems to have got
> transferred to S4 and R.  Often the best approach is to see if a loop is
> fast enough, first.  (In S-PLUS 5.0 lapply was actually slower than a for
> loop.)
>

And I lived all these years in ignorance, thinking I was doing good while I was
making things worse.....


I haven't look at R introduction manuals for a while now, so may be the following
remark is already stated in them. but by the time I started with R, the modern
statistics with S-plus was the main reference and being a poooor student at that
time, I learned things through the internet. It prooves to be a bad thing, since
at that time there was a rumour about these functions being faster than the loops
(like the 'map' function is told to be faster than the for loop in Python for
example).
I just looked at what would get as an answer using a webcrawler, and the rumour
seems to be still alive
(see http://www.math.yorku.ca/Who/Faculty/Monette/S-news/2531.html , at the
bottom of the page, or
http://www.usc.edu/isd/doc/statistics/splus/faq/v5/newinv5.shtml ), but I would
have followed better was has been told on this list I would have known it was not
the case
(see http://www.ens.gu.edu.au/robertk/R/help/00a/1999.html).


Thanks for pointing out the mistake I made,




Laurent



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list