[R] R cycle to calculate distances

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Nov 17 15:33:34 CET 2008


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



More information about the R-help mailing list