[R] Calculating distances between points in a data frame?
Dear list, I would like to calculate the distance between consecutive points in a data frame. Of course the first point in the data frame does not have a point of origin, and should get a value NA. I have tried two different loops, which both result in error: num - seq(0,10,1) X - seq(0,30,3) Y - seq(0,40,4) XY - data.frame(num, X, Y) attach(XY) summary(XY) num X Y Min. : 0.0 Min. : 0.0 Min. : 0 1st Qu.: 2.5 1st Qu.: 7.5 1st Qu.:10 Median : 5.0 Median :15.0 Median :20 Mean : 5.0 Mean :15.0 Mean :20 3rd Qu.: 7.5 3rd Qu.:22.5 3rd Qu.:30 Max. :10.0 Max. :30.0 Max. :40 plot(X,Y) rngNum - range(num) for (i in rngNum){ + XY$DistXY[i] - sqrt( ((X[i]-X[i-1])^2) + ((Y[i]-Y[i-1])^2) ) + } Error in $-.data.frame(`*tmp*`, DistXY, value = sqrt(((X[i] - X[i - : replacement has 10 rows, data has 11 for (i in rngNum){ + XY$DistXY2[i] - ifelse(i=min(rngNum), NA, sqrt(((X[i]-X[i-1])^2) + ((Y[i]-Y[i-1])^2)) ) + } Error in ifelse(i = min(rngNum), NA, sqrt(((X[i] - X[i - 1])^2) + ((Y[i] - : unused argument(s) (i ...) detach(XY) Any suggestions much appreciated, Sander Oom. -- Dr. Sander P. Oom Animal, Plant and Environmental Sciences University of the Witwatersrand Private Bag 3 Wits 2050 South Africa Tel (work) +27 (0)11 717 64 04 Tel (home) +27 (0)18 297 44 51 Fax +27 (0)18 299 24 64 Email [EMAIL PROTECTED] Web www.oomvanlieshout.net/sander __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Calculating distances between points in a data frame?
Hi! It may bee that the function dist can be of some use to you? ?diff I have something like this in mind. (you do not need a loop.) XY$DistXY - sqrt(diff(X)^2+diff(Y)^2) Have fun trying. Sincerely Eryk *** REPLY SEPARATOR *** On 5/31/2004 at 5:25 PM Sander Oom wrote: Dear list, I would like to calculate the distance between consecutive points in a data frame. Of course the first point in the data frame does not have a point of origin, and should get a value NA. I have tried two different loops, which both result in error: num - seq(0,10,1) X - seq(0,30,3) Y - seq(0,40,4) XY - data.frame(num, X, Y) attach(XY) summary(XY) num X Y Min. : 0.0 Min. : 0.0 Min. : 0 1st Qu.: 2.5 1st Qu.: 7.5 1st Qu.:10 Median : 5.0 Median :15.0 Median :20 Mean : 5.0 Mean :15.0 Mean :20 3rd Qu.: 7.5 3rd Qu.:22.5 3rd Qu.:30 Max. :10.0 Max. :30.0 Max. :40 plot(X,Y) rngNum - range(num) for (i in rngNum){ + XY$DistXY[i] - sqrt( ((X[i]-X[i-1])^2) + ((Y[i]-Y[i-1])^2) ) + } Error in $-.data.frame(`*tmp*`, DistXY, value = sqrt(((X[i] - X[i - : replacement has 10 rows, data has 11 for (i in rngNum){ + XY$DistXY2[i] - ifelse(i=min(rngNum), NA, sqrt(((X[i]-X[i-1])^2) + ((Y[i]-Y[i-1])^2)) ) + } Error in ifelse(i = min(rngNum), NA, sqrt(((X[i] - X[i - 1])^2) + ((Y[i] - : unused argument(s) (i ...) detach(XY) Any suggestions much appreciated, Sander Oom. -- Dr. Sander P. Oom Animal, Plant and Environmental Sciences University of the Witwatersrand Private Bag 3 Wits 2050 South Africa Tel (work) +27 (0)11 717 64 04 Tel (home) +27 (0)18 297 44 51 Fax +27 (0)18 299 24 64 Email [EMAIL PROTECTED] Web www.oomvanlieshout.net/sander __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Dipl. bio-chem. Eryk Witold Wolski@MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin 'v' tel: 0049-30-83875219 / \ mail: [EMAIL PROTECTED]---W-Whttp://www.molgen.mpg.de/~wolski __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Calculating distances between points in a data frame?
Try using running from the gregmisc package with pad = TRUE: require(gregmisc) XY - data.frame(num = seq(0,10), X = seq(0,30,3), Y = seq(0, 40, 4) ) DistXY - function(idx) { i - idx[2] with(XY, sqrt( (X[i]-X[i-1])^2 + (Y[i]-Y[i-1])^2 ) ) } XY$Dist - running( 1:nrow(XY), width=2, fun = DistXY, pad = TRUE ) Sander Oom slist at oomvanlieshout.net writes: : : Dear list, : : I would like to calculate the distance between consecutive points in a data : frame. Of course the first point in the data frame does not have a point of : origin, and should get a value NA. I have tried two different loops, which : both result in error: : : num - seq(0,10,1) : X - seq(0,30,3) : Y - seq(0,40,4) : XY - data.frame(num, X, Y) : attach(XY) : summary(XY) :num X Y : Min. : 0.0 Min. : 0.0 Min. : 0 : 1st Qu.: 2.5 1st Qu.: 7.5 1st Qu.:10 : Median : 5.0 Median :15.0 Median :20 : Mean : 5.0 Mean :15.0 Mean :20 : 3rd Qu.: 7.5 3rd Qu.:22.5 3rd Qu.:30 : Max. :10.0 Max. :30.0 Max. :40 : plot(X,Y) : rngNum - range(num) : for (i in rngNum){ : + XY$DistXY[i] - sqrt( ((X[i]-X[i-1])^2) + ((Y[i]-Y[i-1])^2) ) : + } : Error in $-.data.frame(`*tmp*`, DistXY, value = sqrt(((X[i] - X[i - : : replacement has 10 rows, data has 11 : for (i in rngNum){ : + XY$DistXY2[i] - ifelse(i=min(rngNum), NA, sqrt(((X[i]-X[i-1])^2) + : ((Y[i]-Y[i-1])^2)) ) : + } : Error in ifelse(i = min(rngNum), NA, sqrt(((X[i] - X[i - 1])^2) + ((Y[i] - : : unused argument(s) (i ...) : detach(XY) : : : Any suggestions much appreciated, : : Sander Oom. : : -- : Dr. Sander P. Oom : Animal, Plant and Environmental Sciences : University of the Witwatersrand : Private Bag 3 : Wits 2050 : South Africa : : Tel (work) +27 (0)11 717 64 04 : Tel (home) +27 (0)18 297 44 51 : Fax +27 (0)18 299 24 64 : : Email sander at oomvanlieshout.net : Web www.oomvanlieshout.net/sander : : __ : R-help at stat.math.ethz.ch mailing list : https://www.stat.math.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html : : __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Calculating distances between points in a data frame?
Hi Gabor, Thanks for your suggestion. However when installing the package gregmisc, I get the following error: local({a - CRAN.packages() + install.packages(select.list(a[,1],,TRUE), .libPaths()[1], available=a)}) trying URL `http://cran.r-project.org/bin/windows/contrib/1.9/PACKAGES' Content type `text/plain; charset=iso-8859-1' length 17940 bytes opened URL downloaded 17Kb trying URL `http://cran.r-project.org/bin/windows/contrib/1.9/gregmisc_1.11.0.zip' Error in download.file(url, destfile, method, mode = wb) : cannot open URL `http://cran.r-project.org/bin/windows/contrib/1.9/gregmisc_1.11.0.zip' In addition: Warning message: cannot open: HTTP status was `404 Not Found' I was quite surprised as well! I tried different mirrors, but to no avail. Maybe tomorrow, Sander. At 18:44 2004/05/31, you wrote: Try using running from the gregmisc package with pad = TRUE: require(gregmisc) XY - data.frame(num = seq(0,10), X = seq(0,30,3), Y = seq(0, 40, 4) ) DistXY - function(idx) { i - idx[2] with(XY, sqrt( (X[i]-X[i-1])^2 + (Y[i]-Y[i-1])^2 ) ) } XY$Dist - running( 1:nrow(XY), width=2, fun = DistXY, pad = TRUE ) Sander Oom slist at oomvanlieshout.net writes: : : Dear list, : : I would like to calculate the distance between consecutive points in a data : frame. Of course the first point in the data frame does not have a point of : origin, and should get a value NA. I have tried two different loops, which : both result in error: : : num - seq(0,10,1) : X - seq(0,30,3) : Y - seq(0,40,4) : XY - data.frame(num, X, Y) : attach(XY) : summary(XY) :num X Y : Min. : 0.0 Min. : 0.0 Min. : 0 : 1st Qu.: 2.5 1st Qu.: 7.5 1st Qu.:10 : Median : 5.0 Median :15.0 Median :20 : Mean : 5.0 Mean :15.0 Mean :20 : 3rd Qu.: 7.5 3rd Qu.:22.5 3rd Qu.:30 : Max. :10.0 Max. :30.0 Max. :40 : plot(X,Y) : rngNum - range(num) : for (i in rngNum){ : + XY$DistXY[i] - sqrt( ((X[i]-X[i-1])^2) + ((Y[i]-Y[i-1])^2) ) : + } : Error in $-.data.frame(`*tmp*`, DistXY, value = sqrt(((X[i] - X[i - : : replacement has 10 rows, data has 11 : for (i in rngNum){ : + XY$DistXY2[i] - ifelse(i=min(rngNum), NA, sqrt(((X[i]-X[i-1])^2) + : ((Y[i]-Y[i-1])^2)) ) : + } : Error in ifelse(i = min(rngNum), NA, sqrt(((X[i] - X[i - 1])^2) + ((Y[i] - : : unused argument(s) (i ...) : detach(XY) : : : Any suggestions much appreciated, : : Sander Oom. : : -- : Dr. Sander P. Oom : Animal, Plant and Environmental Sciences : University of the Witwatersrand : Private Bag 3 : Wits 2050 : South Africa : : Tel (work) +27 (0)11 717 64 04 : Tel (home) +27 (0)18 297 44 51 : Fax +27 (0)18 299 24 64 : : Email sander at oomvanlieshout.net : Web www.oomvanlieshout.net/sander : : __ : R-help at stat.math.ethz.ch mailing list : https://www.stat.math.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html : : __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Calculating distances between points in a data frame?
Sander Oom wrote: Hi Gabor, Thanks for your suggestion. However when installing the package gregmisc, I get the following error: local({a - CRAN.packages() + install.packages(select.list(a[,1],,TRUE), .libPaths()[1], available=a)}) trying URL `http://cran.r-project.org/bin/windows/contrib/1.9/PACKAGES' Content type `text/plain; charset=iso-8859-1' length 17940 bytes opened URL downloaded 17Kb trying URL `http://cran.r-project.org/bin/windows/contrib/1.9/gregmisc_1.11.0.zip' Error in download.file(url, destfile, method, mode = wb) : cannot open URL `http://cran.r-project.org/bin/windows/contrib/1.9/gregmisc_1.11.0.zip' In addition: Warning message: cannot open: HTTP status was `404 Not Found' I was quite surprised as well! I tried different mirrors, but to no avail. Maybe tomorrow, Sander. There is a bug in the script that uploads binary packages to CRAN. The PACKAGES file for R-1.9.x/R-2.0.x for Windows erroneously indicates that gregmisc_1.11.0.zip is available, but it isn't (at least the PACKAGES file will be fixed tomorrow). gregmisc does not pass the checks (well, it even does not install) on Windows, see: CRAN/bin/windows/contrib/checkSummaryWin.html There is a note on that page which points you to an outdated but still available Windows version: CRAN/bin/windows/contrib/1.9/last/gregmisc_0.10.2.zip Uwe Ligges At 18:44 2004/05/31, you wrote: Try using running from the gregmisc package with pad = TRUE: require(gregmisc) XY - data.frame(num = seq(0,10), X = seq(0,30,3), Y = seq(0, 40, 4) ) DistXY - function(idx) { i - idx[2] with(XY, sqrt( (X[i]-X[i-1])^2 + (Y[i]-Y[i-1])^2 ) ) } XY$Dist - running( 1:nrow(XY), width=2, fun = DistXY, pad = TRUE ) Sander Oom slist at oomvanlieshout.net writes: : : Dear list, : : I would like to calculate the distance between consecutive points in a data : frame. Of course the first point in the data frame does not have a point of : origin, and should get a value NA. I have tried two different loops, which : both result in error: : : num - seq(0,10,1) : X - seq(0,30,3) : Y - seq(0,40,4) : XY - data.frame(num, X, Y) : attach(XY) : summary(XY) :num X Y : Min. : 0.0 Min. : 0.0 Min. : 0 : 1st Qu.: 2.5 1st Qu.: 7.5 1st Qu.:10 : Median : 5.0 Median :15.0 Median :20 : Mean : 5.0 Mean :15.0 Mean :20 : 3rd Qu.: 7.5 3rd Qu.:22.5 3rd Qu.:30 : Max. :10.0 Max. :30.0 Max. :40 : plot(X,Y) : rngNum - range(num) : for (i in rngNum){ : + XY$DistXY[i] - sqrt( ((X[i]-X[i-1])^2) + ((Y[i]-Y[i-1])^2) ) : + } : Error in $-.data.frame(`*tmp*`, DistXY, value = sqrt(((X[i] - X[i - : : replacement has 10 rows, data has 11 : for (i in rngNum){ : + XY$DistXY2[i] - ifelse(i=min(rngNum), NA, sqrt(((X[i]-X[i-1])^2) + : ((Y[i]-Y[i-1])^2)) ) : + } : Error in ifelse(i = min(rngNum), NA, sqrt(((X[i] - X[i - 1])^2) + ((Y[i] - : : unused argument(s) (i ...) : detach(XY) : : : Any suggestions much appreciated, : : Sander Oom. : : -- : Dr. Sander P. Oom : Animal, Plant and Environmental Sciences : University of the Witwatersrand : Private Bag 3 : Wits 2050 : South Africa : : Tel (work) +27 (0)11 717 64 04 : Tel (home) +27 (0)18 297 44 51 : Fax +27 (0)18 299 24 64 : : Email sander at oomvanlieshout.net : Web www.oomvanlieshout.net/sander : : __ : R-help at stat.math.ethz.ch mailing list : https://www.stat.math.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html : : __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html