[R] multivariate distance and apply

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jul 23 18:17:29 CEST 2001

On Mon, 23 Jul 2001, Agustin Lobo wrote:

> Hi!
> I'm computing a mutivariate distance that is
> not among the different options of dist. Hence
> I've written
> midistancia(v1,v2)
> where v1 and v2 are vectors.
> Now, I have to calculate the distances between
> one given vector (V1) and a number of vectors
> arranged as the rows of a matrix (stat). I'm
> using
> apply(stat, 1, midistancia, V1)
> to apply midstancia to each row of stat.
> The question is: is this the most efficient method
> in R or is there a faster way to calculate midistancia
> between V1 and the rows of stat?
> (stat can become ~80000 rows x 7 cols)

It entirely depends on what the distance actually is and if midistancia()
could be vectorized.  apply() in R just runs a for loop, and using for()
directly is probably slightly more efficient when the result (as here) is
a simple vector.

You need to supply more details!

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

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