title: Calculate distance between points using gdist() for grouped subsets
body: <p>A subset of my data for 2 individuals (<code>squirrelID</code>) can be found <a href="https://figshare.com/s/ace5b44bc12394a7c46d" rel="nofollow noreferrer">here</a>.</p>
  
  <p>My data looks as follows (only showing the relevant columns):</p>
  
  <pre><code>lat                  lon                NatalMidden   squirrelID    type
  60.9577819984406    -138.0347849708050  -27           NA            Nest2017
  60.9574120212346    -138.0345689691600  -27           NA            NatalMidden
  60.9578209742904    -138.0346520338210  -27           23054         Foray
  60.9575380012393    -138.0348329991100  -27           23054         Foray
  60.9576250053942    -138.0339069664480  -27           23054         Foray
  60.957643026486     -138.0338829942050  -27           23054         Foray
  60.9575670026243    -138.0348739866170  -27           23054         Foray
  </code></pre>
  
  <p>For example, for <code>squirrelID</code> 23054, it was located (<code>Foray</code>) multiple times (<code>type</code> column) and I have a corresponding latitude (<code>lat</code>) and longitude (<code>lon</code>) for each <code>Foray</code>. I am trying to calculate the distance between each <code>Foray</code> (<code>type</code> column) and <code>Nest2017</code> (<code>type</code> column) for each individual (<code>squirrelID</code>) separately.</p>
  
  <p>The below code works (and gives me a value of 15.11501 m), but it requires that I manually enter each point. This is not problematic, per say, but I am working with +2000 observations with more than 2 options per <code>grid</code>, <code>NatalMidden</code>, and <code>squirrelID</code> columns.</p>
  
  <pre><code>library(Imap)
  
  gdist(60.9578209742904,-138.0346520338210, 60.9577819984406, -138.0347849708050, units="m", verbose=FALSE)
  </code></pre>
  
  <p><strong>Is there a way I could work within the</strong> <code>dplyr</code> <strong>framework to</strong> <code>group_by(squirrelID)</code> <strong>and then calculate the distances between each</strong> <code>Foray</code> <strong>and its corresponding</strong> <code>Nest2017</code> <strong>(which has the same</strong> <code>NatalMidden</code> <strong>for both the</strong> <code>Foray</code> <strong>and</strong> <code>Nest2017</code><strong>)?</strong></p>
  
  <p>My ultimate goal is to create a new column for the distance between the <code>Foray</code> and <code>Nest2017</code> for each <code>squirrelID</code>.</p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>I have tried the following:</p>
  
  <pre><code>nests&lt;-df %&gt;% #creating a new data frame for Nest2017 points only
      filter(type %in% "Nest2017") %&gt;%
      select(ID,lat,lon,ele,grid,NatalMidden,type)
  
  foray&lt;-df %&gt;% #creating a new data frame for Foray points only
      filter(type %in% "Foray") %&gt;%
      mutate(sq_id=as.factor(sq_id)) %&gt;%
      group_by(sq_id)
  </code></pre>
  
  <p>But these subsets do not work in the <code>gdist</code> function (I get this error):</p>
  
  <pre><code>gdist(nests$lat, nests$lon, foray$lat, foray$lon, units="m", verbose=FALSE)
  
  Error in while (abs(lamda - lamda.old) &gt; 1e-11) { : 
    missing value where TRUE/FALSE needed
  In addition: Warning messages:
  1: In Ops.factor(lon.1, rad) : ‘*’ not meaningful for factors
  2: In Ops.factor(lat.1, rad) : ‘*’ not meaningful for factors
  3: In Ops.factor(lon.2, rad) : ‘*’ not meaningful for factors
  4: In Ops.factor(lat.2, rad) : ‘*’ not meaningful for factors
  5: In lon.1 - lon.2 :
    longer object length is not a multiple of shorter object length
  6: In while (abs(lamda - lamda.old) &gt; 1e-11) { :
    the condition has length &gt; 1 and only the first element will be used
  </code></pre>