title: How to join tibbles/dataframes by using the 'nearest time' of a grouping column in R?
body: <p>I have this dataset exemplified below:</p>
  <pre><code>library(lubridate)
  library(tidyverse)
  
  id &lt;- c(&quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;)
  date &lt;- ymd(c(&quot;2017-11-26&quot;, &quot;2017-11-26&quot;, &quot;2017-11-26&quot;, &quot;2017-11-29&quot;))
  time &lt;- hms(c(&quot;09:25:30&quot;, &quot;10:10:02&quot;, &quot;09:15:36&quot;, &quot;9:50:55&quot;))
  variable1 &lt;- c(&quot;67&quot;, &quot;30&quot;, &quot;28&quot;, &quot;90&quot;)
  df &lt;- tibble(id, date, time, variable1)
  df
  
  A tibble: 4 x 4
    id    date       time       variable1
    &lt;chr&gt; &lt;date&gt;     &lt;Period&gt;   &lt;chr&gt;    
  1  A     2017-11-26 9H 25M 30S 67       
  2  A     2017-11-26 10H 10M 2S 30       
  3  B     2017-11-26 9H 15M 36S 28       
  4  B     2017-11-29 9H 50M 55S 90   
  </code></pre>
  <p>I need to add for each row in this dataset two new variables (latitude and longitude) based on a second dataset, using the columns &quot;id&quot;, &quot;date&quot; and &quot;time&quot; as conditions to create the new columns.</p>
  <p>However, the &quot;time&quot; is not exactly the same between the two datasets, and thus I need this condition to be an approximation (i.e. the nearest 'time' available for a given 'id' and 'date'). Another issue is that the second dataset has more rows, and thus, by selecting the 'nearest time', some rows will be ignored when adding to the first dataset.</p>
  <p>Please see this example for the second dataset:</p>
  <pre><code>id &lt;- c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;)
  date &lt;- ymd(c(&quot;2017-11-26&quot;, &quot;2017-11-26&quot;, &quot;2017-11-30&quot;, &quot;2017-11-26&quot;,         
  &quot;2017-11-26&quot;, &quot;2017-11-29&quot;))
  time &lt;- hms(c(&quot;09:00:00&quot;, &quot;10:00:00&quot;, &quot;08:00:00&quot;, &quot;09:00:00&quot;, 
  &quot;13:00:00&quot;, &quot;10:00:00&quot;))
  lat &lt;- c(-30.6456, -29.5648, -27.6667, -31.5587, -30.6934, -29.3147)
  long &lt;- c(-50.4879, -49.8715, -51.8716, -50.4456, -50.9842, -51.9787)
  df2 &lt;- tibble(id, date, time, lat, long)
  df2
  
  # A tibble: 6 x 5
   id    date       time        lat  long
   &lt;chr&gt; &lt;date&gt;     &lt;Period&gt;  &lt;dbl&gt; &lt;dbl&gt;
  1  A   2017-11-26 9H 0M 0S  -30.6 -50.5
  2  A   2017-11-26 10H 0M 0S -29.6 -49.9
  3  A   2017-11-30 8H 0M 0S  -27.7 -51.9
  4  B   2017-11-26 9H 0M 0S  -31.6 -50.4
  5  B   2017-11-26 13H 0M 0S -30.7 -51.0
  6  B   2017-11-29 10H 0M 0S -29.3 -52.0  
  </code></pre>
  <p>Therefore, I first need to group by &quot;id&quot;, then by &quot;date&quot;, and then extract the &quot;lat&quot; and &quot;long&quot; in  the dataset 2 for the nearest &quot;time&quot; in the dataset 1, creating the two new columns in the dataset 1.</p>
  <p>The resulting dataset would look like this:</p>
  <pre><code>df_output
  # A tibble: 4 x 6
    id    date       time       variable1   lat  long
   &lt;chr&gt; &lt;date&gt;     &lt;Period&gt;   &lt;chr&gt;     &lt;dbl&gt; &lt;dbl&gt;
  1  A    2017-11-26 9H 25M 30S 67      -30.6 -50.5
  2  A    2017-11-26 10H 10M 2S 30      -29.6 -49.9
  3  B    2017-11-26 9H 15M 36S 28      -31.6 -50.4
  4  B    2017-11-29 9H 50M 55S 90      -29.3 -52.0
  </code></pre>
  <p>I've tried with group_by(), map2_dfr(), merge(), left_join(), case_when(), etc.. but couldn't find how to do this.</p>