We need to measure distance between points.

The Data

Here we will use the enigmatic Araptus attenuata data set as sf objects. See point construction for a discussion on the differences between data.frame and sf objects and point projections for how to deal with different projections.

## Simple feature collection with 29 features and 4 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: -114.2935 ymin: 23.2855 xmax: -109.327 ymax: 29.32541
## epsg (SRID):    3857
## proj4string:    +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs
## First 10 features:
##    Site Males Females Suitability                   geometry
## 1    12    24      21   0.3519050 POINT (-112.6655 27.18232)
## 2   153    35      41   0.7324870 POINT (-110.4624 24.13389)
## 3   157    26      30   0.8810290   POINT (-110.096 24.0195)
## 4   159    22      15   0.1879650 POINT (-113.3161 27.52944)
## 5   160    48      36   0.3651910 POINT (-112.5296 27.40498)
## 6   161    64      63   0.2791050   POINT (-112.986 27.0367)
## 7   162    57      41   0.6136198   POINT (-112.408 27.2028)
## 8   163    21      21   0.4328730   POINT (-110.951 24.2115)
## 9   166    19      26   0.2673030 POINT (-112.0806 25.91409)
## 10  168    28      25   0.4964650 POINT (-111.2156 25.55757)

Distance

Measuring the distances

## Units: [m]
##  [1] 2.3741217 1.5046721 1.8718088 2.9944781 2.5290427 2.4014298 2.2981315
##  [8] 1.0887930 0.9775184 0.7035419 3.5358992 3.5885231 4.1240004 4.3110594
## [15] 2.8487352 1.6197284 0.3319444 1.0412707 0.6771468 2.0885250 1.0263484
## [22] 0.9944142 4.4169443 5.0351570 3.4708668 4.5903515 1.9567086 2.3434902
## [29] 0.0000000

Notice that the distances here are in an odd unit.

## Units: [m]
##  [1] 2.3741217 1.5046721 1.8718088 2.9944781 2.5290427 2.4014298 2.2981315
##  [8] 1.0887930 0.9775184 0.7035419 3.5358992 3.5885231 4.1240004 4.3110594
## [15] 2.8487352 1.6197284 0.3319444 1.0412707 0.6771468 2.0885250 1.0263484
## [22] 0.9944142 4.4169443 5.0351570 3.4708668 4.5903515 1.9567086 2.3434902
## [29] 0.0000000