[R] R cycle to calculate distances

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Nov 17 17:37:57 CET 2008


On Mon, 2008-11-17 at 07:20 -0800, P.Branco wrote:
> Sorry, it does not work.
> 
> If I do a rnorm I lose the original values of my vectors, and the equation
> result must be attained by the use of the original values.

Dimitris was generating some dummy data to test that the function
worked. He had to do this because you didn't provide the data to make up
r1, r2, etc.

You don't need to do the calls to rnorm, just use your original data,
stored in those variables/objects.

If r1, r2 already contain your data, you just need to do:

dd <- sqrt(0.723523 * outer(r1, s1, "-")^2 + 0.215518 * outer(r2, s2, "-")^2)
dd

Does that work?

G

> 
> Thanks,
> P.Branco
> 
> 
> Dimitris Rizopoulos-4 wrote:
> > 
> > try this (presented only for two pairs):
> > 
> > r1 <- rnorm(49)
> > r2 <- rnorm(49)
> > s1 <- rnorm(300)
> > s2 <- rnorm(300)
> > 
> > dd <- sqrt(0.723523 * outer(r1, s1, "-")^2 + 0.215518 * outer(r2, s2, 
> > "-")^2)
> > dd
> > 
> > I hope it helps.
> > 
> > Best,
> > Dimitris
> > 
> > P.Branco wrote:
> >> Dear all,
> >> 
> >> I am for the first time trying to work with R, but I have bumped into a
> >> problem.
> >> 
> >> I have four vectors:
> >> r1 (49 values)
> >> r21 (49 values)
> >> r22 (49 values)
> >> r3 (49 values)
> >> s1 (300 values)
> >> s21 (300 values)
> >> s22 (300 values)
> >> s3 (300 values)
> >> 
> >> And I would like to calculate the distances from all references (r) sites
> >> to
> >> sample (s) site, for that I wrote this equation:
> >> 
> >> drs=sqrt(((0.723523*(r1[i]-s1[ii]))^2)+(((-0.215518*(r21[i]-s21[ii]))^2)+((0.518612*(r22[i]-s22[ii]))^2))+((0.283655*(r3[i]-s3[ii]))^2))
> >> 
> >> Now how can I run a cycle in order to calculate the distances?
> >> 
> >> Could you please help me?
> >> 
> >> P. Branco
> >> 
> > 
> > -- 
> > Dimitris Rizopoulos
> > Assistant Professor
> > Department of Biostatistics
> > Erasmus Medical Center
> > 
> > Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
> > Tel: +31/(0)10/7043478
> > Fax: +31/(0)10/7043014
> > 
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> > 
> > 
> 
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list