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 -- View this message in context: http://www.nabble.com/R-cycle-to-calculate-distances-tp20540340p20540340.html Sent from the R help mailing list archive at Nabble.com.
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
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. 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. > >-- View this message in context: http://www.nabble.com/R-cycle-to-calculate-distances-tp20540340p20541413.html Sent from the R help mailing list archive at Nabble.com.