[R] vector angle

Laurent Gautier laurent at cbs.dtu.dk
Tue Jul 17 09:49:07 CEST 2001

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.

About the NAs, you may want to ignore the vectors which have a NA coordinate, or
may be do something else...
to have a foot in this, you may try the help for the functions 'is.na' and

I hope it helps,


Laurent Gautier                 CBS, Building 208, DTU
PhD. Student                    D-2800 Lyngby,Denmark
tel: +45 45 25 24 85            http://www.cbs.dtu.dk/laurent

-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://stat.ethz.ch/pipermail/r-help/attachments/20010717/a5a3487b/attachment.html

More information about the R-help mailing list