<!-- Generated by pkgdown: do not edit by hand -->
<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Find the nearest lat/long to another lat/long — nearest • maxcovr</title>

<!-- jquery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<!-- Bootstrap -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/flatly/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha256-U5ZEeKfGNOja007MMD3YBI0A3OSZOQbeG6z2f2Y0hu8=" crossorigin="anonymous"></script>

<!-- Font Awesome icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous" />

<!-- clipboard.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js" integrity="sha256-FiZwavyI2V6+EXO1U+xzLG3IKldpiTFf3153ea9zikQ=" crossorigin="anonymous"></script>

<!-- sticky kit -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/sticky-kit/1.1.3/sticky-kit.min.js" integrity="sha256-c4Rlo1ZozqTPE2RLuvbusY3+SU1pQaJC0TjuhygMipw=" crossorigin="anonymous"></script>

<!-- pkgdown -->
<link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script>



<meta property="og:title" content="Find the nearest lat/long to another lat/long — nearest" />

<meta property="og:description" content="This function finds the nearest lat/long pairs to another lat/long pair.
So in the york building and york crime context, writing
nearest(york_crime,york) reads as &quot;find the nearest crime in york to
each building in york, and returns a dataframe with every building in york,
the nearest york_crime to each building, and the distance in metres between
the two. Likewise, you could write nearest(york, york_crime), and this
would return the nearest building to every crime. nearest assumes that
the names of the latitude and longitude are &quot;lat&quot; and &quot;long&quot;, but you can
provide these names." />
<meta name="twitter:card" content="summary" />



<!-- mathjax -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script>

<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->


  </head>

  <body>
    <div class="container template-reference-topic">
      <header>
      <div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">maxcovr</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">0.1.3.9200</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li>
  <a href="../index.html">
    <span class="fa fa-home fa-lg"></span>
     
  </a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
    Articles
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
    <li>
      <a href="../articles/cross_validation_with_maxcovr.html">Using Cross Validation with maxcovr</a>
    </li>
    <li>
      <a href="../articles/intro_to_maxcovr.html">Using maxcovr</a>
    </li>
    <li>
      <a href="../articles/using_max_coverage_relocation.html">Using max_coverage_relocation</a>
    </li>
  </ul>
</li>
<li>
  <a href="../news/index.html">Changelog</a>
</li>
      </ul>
      
      <ul class="nav navbar-nav navbar-right">
        <li>
  <a href="https://github.com/njtierney/maxcovr">
    <span class="fa fa-github fa-lg"></span>
     
  </a>
</li>
      </ul>
      
    </div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

      
      </header>

<div class="row">
  <div class="col-md-9 contents">
    <div class="page-header">
    <h1>Find the nearest lat/long to another lat/long</h1>
    <small class="dont-index">Source: <a href='https://github.com/njtierney/maxcovr/blob/master/R/nearest.R'><code>R/nearest.R</code></a></small>
    <div class="hidden name"><code>nearest.Rd</code></div>
    </div>

    <div class="ref-description">
    
    <p>This function finds the nearest lat/long pairs to another lat/long pair.
So in the york building and york crime context, writing
<code>nearest(york_crime,york)</code> reads as "find the nearest crime in york to
each building in york, and returns a dataframe with every building in york,
the nearest york_crime to each building, and the distance in metres between
the two. Likewise, you could write <code>nearest(york, york_crime)</code>, and this
would return the nearest building to every crime. <code>nearest</code> assumes that
the names of the latitude and longitude are "lat" and "long", but you can
provide these names.</p>
    
    </div>

    <pre class="usage"><span class='fu'>nearest</span>(<span class='no'>nearest_df</span>, <span class='no'>to_df</span>, <span class='kw'>nearest_lat</span> <span class='kw'>=</span> <span class='st'>"lat"</span>, <span class='kw'>nearest_long</span> <span class='kw'>=</span> <span class='st'>"long"</span>,
  <span class='kw'>to_lat</span> <span class='kw'>=</span> <span class='st'>"lat"</span>, <span class='kw'>to_long</span> <span class='kw'>=</span> <span class='st'>"long"</span>)</pre>
    
    <h2 class="hasAnchor" id="arguments"><a class="anchor" href="#arguments"></a>Arguments</h2>
    <table class="ref-arguments">
    <colgroup><col class="name" /><col class="desc" /></colgroup>
    <tr>
      <th>nearest_df</th>
      <td><p>a dataframe containing latitude and longitude</p></td>
    </tr>
    <tr>
      <th>to_df</th>
      <td><p>a dataframe containing latitude and longitude</p></td>
    </tr>
    <tr>
      <th>nearest_lat</th>
      <td><p>name of latitude in nearest_df</p></td>
    </tr>
    <tr>
      <th>nearest_long</th>
      <td><p>name of longitude in nearest_df</p></td>
    </tr>
    <tr>
      <th>to_lat</th>
      <td><p>name of latitude in to_df</p></td>
    </tr>
    <tr>
      <th>to_long</th>
      <td><p>name of longitude in to_df</p></td>
    </tr>
    </table>
    
    <h2 class="hasAnchor" id="value"><a class="anchor" href="#value"></a>Value</h2>

    <p>dataframe of "to_df" along with the nearest "nearest_df" to each row,
along with the distance between the two, and the nearest_id, the row
position of the nearest_df closest to that row.</p>
    

    <h2 class="hasAnchor" id="examples"><a class="anchor" href="#examples"></a>Examples</h2>
    <pre class="examples"><div class='input'>
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/library'>library</a></span>(<span class='no'>maxcovr</span>)

<span class='fu'>nearest</span>(<span class='kw'>nearest_df</span> <span class='kw'>=</span> <span class='no'>york_crime</span>,
        <span class='kw'>to_df</span> <span class='kw'>=</span> <span class='no'>york</span>)</div><div class='output co'>#&gt; <span style='color: #555555;'># A tibble: 2,944 x 22</span><span>
#&gt;    to_id nearest_id distance long_to lat_to object_id desig_id pref_ref name 
#&gt;    </span><span style='color: #555555;font-style: italic;'>&lt;dbl&gt;</span><span>      </span><span style='color: #555555;font-style: italic;'>&lt;dbl&gt;</span><span>    </span><span style='color: #555555;font-style: italic;'>&lt;dbl&gt;</span><span>   </span><span style='color: #555555;font-style: italic;'>&lt;dbl&gt;</span><span>  </span><span style='color: #555555;font-style: italic;'>&lt;dbl&gt;</span><span>     </span><span style='color: #555555;font-style: italic;'>&lt;int&gt;</span><span> </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span>       </span><span style='color: #555555;font-style: italic;'>&lt;int&gt;</span><span> </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span>
#&gt; </span><span style='color: #555555;'> 1</span><span>     1         33    36.0    -</span><span style='color: #BB0000;'>1.09</span><span>   54.0      </span><span style='text-decoration: underline;'>6</span><span>144 DYO1195    </span><span style='text-decoration: underline;'>463</span><span>280 GUIL…
#&gt; </span><span style='color: #555555;'> 2</span><span>     2        183     4.30   -</span><span style='color: #BB0000;'>1.09</span><span>   54.0      </span><span style='text-decoration: underline;'>6</span><span>143 DYO1746        </span><span style='color: #BB0000;'>NA</span><span> Boot…
#&gt; </span><span style='color: #555555;'> 3</span><span>     3        183    35.8    -</span><span style='color: #BB0000;'>1.09</span><span>   54.0      </span><span style='text-decoration: underline;'>6</span><span>142 DYO1373    </span><span style='text-decoration: underline;'>462</span><span>942 BOOT…
#&gt; </span><span style='color: #555555;'> 4</span><span>     4        183    89.1    -</span><span style='color: #BB0000;'>1.08</span><span>   54.0      </span><span style='text-decoration: underline;'>6</span><span>141 DYO1745        </span><span style='color: #BB0000;'>NA</span><span> Boot…
#&gt; </span><span style='color: #555555;'> 5</span><span>     5        942   236.     -</span><span style='color: #BB0000;'>1.10</span><span>   53.9      </span><span style='text-decoration: underline;'>6</span><span>140 DYO14      </span><span style='text-decoration: underline;'>325</span><span>952 CHUR…
#&gt; </span><span style='color: #555555;'> 6</span><span>     6        942   236.     -</span><span style='color: #BB0000;'>1.10</span><span>   53.9      </span><span style='text-decoration: underline;'>6</span><span>139 DYO14      </span><span style='text-decoration: underline;'>325</span><span>952 CHUR…
#&gt; </span><span style='color: #555555;'> 7</span><span>     7        245    60.6    -</span><span style='color: #BB0000;'>1.08</span><span>   54.0      </span><span style='text-decoration: underline;'>6</span><span>138 DYO1226    </span><span style='text-decoration: underline;'>463</span><span>228 NUMB…
#&gt; </span><span style='color: #555555;'> 8</span><span>     8        264    60.9    -</span><span style='color: #BB0000;'>1.08</span><span>   54.0      </span><span style='text-decoration: underline;'>6</span><span>137 DYO1226    </span><span style='text-decoration: underline;'>463</span><span>228 NUMB…
#&gt; </span><span style='color: #555555;'> 9</span><span>     9       </span><span style='text-decoration: underline;'>1</span><span>106    91.8    -</span><span style='color: #BB0000;'>1.08</span><span>   54.0      </span><span style='text-decoration: underline;'>6</span><span>136 DYO421     </span><span style='text-decoration: underline;'>464</span><span>726 TERR…
#&gt; </span><span style='color: #555555;'>10</span><span>    10        184    23.9    -</span><span style='color: #BB0000;'>1.08</span><span>   54.0      </span><span style='text-decoration: underline;'>6</span><span>135 DYO978     </span><span style='text-decoration: underline;'>463</span><span>684 </span><span style='color: #BB0000;'>NA</span><span>   
#&gt; </span><span style='color: #555555;'># … with 2,934 more rows, and 13 more variables: grade </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span style='color: #555555;'>, category </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span style='color: #555555;'>,
#&gt; #   persistent_id </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span style='color: #555555;'>, date </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span style='color: #555555;'>, lat_nearest </span><span style='color: #555555;font-style: italic;'>&lt;dbl&gt;</span><span style='color: #555555;'>, long_nearest </span><span style='color: #555555;font-style: italic;'>&lt;dbl&gt;</span><span style='color: #555555;'>,
#&gt; #   street_id </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span style='color: #555555;'>, street_name </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span style='color: #555555;'>, context </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span style='color: #555555;'>, id </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span style='color: #555555;'>,
#&gt; #   location_type </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span style='color: #555555;'>, location_subtype </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span style='color: #555555;'>, outcome_status </span><span style='color: #555555;font-style: italic;'>&lt;chr&gt;</span><span></div><div class='input'>
# you can use the pipe as well

</div></span><span class='co'># NOT RUN {</span>
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/library'>library</a></span>(<span class='no'>magrittr</span>)
<span class='no'>york_crime</span> <span class='kw'>%&gt;%</span> <span class='fu'>nearest</span>(<span class='no'>york</span>)

<span class='co'># }</span><div class='input'>
</div></pre>
  </div>
  <div class="col-md-3 hidden-xs hidden-sm" id="sidebar">
    <h2>Contents</h2>
    <ul class="nav nav-pills nav-stacked">
      <li><a href="#arguments">Arguments</a></li>
      
      <li><a href="#value">Value</a></li>
      
      <li><a href="#examples">Examples</a></li>
    </ul>

  </div>
</div>

      <footer>
      <div class="copyright">
  <p>Developed by Nicholas Tierney.</p>
</div>

<div class="pkgdown">
  <p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.3.0.</p>
</div>
      </footer>
   </div>

  

  </body>
</html>

