title: How to copy data from 1 dataframe into another based on several conditions
body: <p>In my <code>df1</code> (including <code>df1$id</code>, <code>df1$datetime_interval</code>, <code>df1$datetime_event</code> and <code>df1$event</code>) i'd like to put data from <code>df2</code> (including <code>df2$id</code>, <code>df2$datetime_event</code>) based onder these conditions:</p>
  
  <p><strong>if</strong> <code>df1$id</code> and <code>df2$id</code> match </p>
  
  <p><strong>and</strong> if <code>df2$datetime_event</code> is within the <code>df1$datetime_interval</code>,</p>
  
  <p><strong>than</strong> I want the data of <code>df2$datetime_event</code> copied in the column of <code>df1$datetime_event</code>, of that perticular row in <code>df1</code>, <strong>and</strong> a string (for instance "yes") in <code>df1$event</code>.</p>
  
  <p>if the conditions aren't met, I want no results (NA)</p>
  
  <p>So:</p>
  
  <pre><code>df1
  ID        datetime_interval                                  datetime_event    event
  1       2019-04-19 21:50:00 UTC--2019-04-20 21:31:00 UTC           NA            NA
  1       2019-07-02 04:23:00 UTC--2019-07-02 08:51:00 UTC           NA            NA
  2       2019-07-04 19:45:00 UTC--2019-07-05 00:30:00 UTC           NA            NA
  3       2019-06-07 08:55:00 UTC--2019-06-07 14:43:00 UTC           NA            NA
  3       2019-05-06 17:18:00 UTC--2019-05-06 23:18:00 UTC           NA            NA
  6       2019-08-02 22:00:00 UTC--2019-08-04 03:10:00 UTC           NA            NA
  </code></pre>
  
  <pre><code>df2
  ID        datetime_event                                  
  1       2019-04-19 21:55:00        
  3       2019-05-06 21:23:00 
  5       2019-07-04 19:45:00 
  6       2019-05-06 17:18:00
  6       2019-08-03 10:10:00            
  </code></pre>
  
  <p>I have tried some things but it didn't work out like i want it too. I'm still missing some steps and i don't know how to move on from this. This is what i have so far:</p>
  
  <pre><code>for(i in seq_along(df1$id)){
    for(j in seq_along(df2$id)){
      ifelse(df2$id[j] ==  df1$id[i]) {
         ifelse(df2$datetime_event[j] %within% df1$datetime_interval[i] == TRUE){
          df1$datetime_event &lt;- df2$datetime_ic_corr[j]
         }
       }
     }
   } 
  </code></pre>
  
  <p>my desired outcome is this:</p>
  
  <pre><code>df1
  ID        datetime_event                                      datetime_event          event
  1       2019-04-19 21:50:00 UTC--2019-04-20 21:31:00 UTC    2019-04-19 21:55:00       yes
  1       2019-07-02 04:23:00 UTC--2019-07-02 08:51:00 UTC           NA                  NA
  2       2019-07-04 19:45:00 UTC--2019-07-05 00:30:00 UTC           NA                  NA
  3       2019-06-07 08:55:00 UTC--2019-06-07 14:43:00 UTC           NA                  NA
  3       2019-05-06 17:18:00 UTC--2019-05-06 23:18:00 UTC    2019-05-06 21:23:00        yes
  6       2019-08-02 22:00:00 UTC--2019-08-04 03:10:00 UTC    2019-08-03 10:10:00        yes
  </code></pre>
  
  <p>Thank you in advance for all new input! Cause I'm stuck...</p>
  
  <pre><code>dput(df1)
  structure(list(ID = c(1, 1, 2, 3, 3, 6), datetime_interval = c("2019-04-19 21:50:00 UTC--2019-04-20 21:31:00 UTC", 
  "2019-07-02 04:23:00 UTC--2019-07-02 08:51:00 UTC", "2019-07-04 19:45:00 UTC--2019-07-05 00:30:00 UTC", 
  "2019-06-07 08:55:00 UTC--2019-06-07 14:43:00 UTC", "2019-05-06 17:18:00 UTC--2019-05-06 23:18:00 UTC", 
  "2019-08-02 22:00:00 UTC--2019-08-04 03:10:00 UTC"), datetime_event = c("NA", 
  "NA", "NA", "NA", "NA", "NA"), event = c("NA", "NA", "NA", "NA", 
  "NA", "NA")), row.names = c(NA, -6L), class = c("tbl_df", "tbl", 
  "data.frame"))
  
  dput(df2)
  structure(list(ID = c(1, 3, 5, 6, 6), datetime_event = c("2019-04-19 21:55:00 UTC", 
  "2019-05-06 21:23:00 UTC", "2019-07-04 19:45:00 UTC", "2019-05-06 17:18:00 UTC", 
  "2019-08-03 10:10:00 UTC")), row.names = c(NA, -5L), class = c("tbl_df", 
  "tbl", "data.frame"))
  </code></pre>