title: Trying to determine if two ranges of dates overlap using R
body: <p>I have a dataset that includes information about the schools that a student has attended within an academic year and their entry and withdrawal dates from each school. While most students only attend one school, there are others who have attended up to four different schools. I would like to make sure that none of the date ranges overlap. Below is an example of the data that I have (the dates are structured as dates):</p>
  <pre><code>|---------------------|------------------|---------------------|------------------|  
  |    entry_date_1     | withdrawal_date_1|    entry_date_2     | withdrawal_date_2|  
  |---------------------|------------------|---------------------|------------------|  
  |     2017-11-09      |     2018-05-24   |          NA         |         NA       |  
  |---------------------|------------------|---------------------|------------------|   
  |     2017-08-14      |     2017-12-15   |    2017-12-16       |    2018-05-24    |  
  |---------------------|------------------|---------------------|------------------|  
  |     2017-08-14      |     2018-06-01   |    2018-01-16       |    2018-03-20    |        
  |---------------------|------------------|---------------------|------------------|  
  |     2018-01-24      |     2018-02-25   |    2018-04-03       |    2018-05-24    |  
  |---------------------|------------------|---------------------|------------------|  
  </code></pre>
  <p>What I would ideally like is a column that gives me a logical operator like this:</p>
  <pre><code>|---------------------|------------------|---------------------|------------------|------------------|  
  |    entry_date_1     | withdrawal_date_1|    entry_date_2     | withdrawal_date_2|     overlap?     |  
  |---------------------|------------------|---------------------|------------------|------------------|  
  |     2017-11-09      |     2018-05-24   |          NA         |         NA       |       NA         |  
  |---------------------|------------------|---------------------|------------------|------------------|   
  |     2017-08-14      |     2017-12-15   |    2017-12-16       |    2018-05-24    |       FALSE      |  
  |---------------------|------------------|---------------------|------------------|------------------|  
  |     2017-08-14      |     2018-06-01   |    2018-01-16       |    2018-03-20    |       TRUE       |        
  |---------------------|------------------|---------------------|------------------|------------------|  
  |     2018-01-24      |     2018-02-25   |    2018-04-03       |    2018-05-24    |       FALSE      |  
  |---------------------|------------------|---------------------|------------------|------------------| 
  </code></pre>
  <p>I tried doing this using the %overlaps% function in the DescTools package, but it doesn't yield a logical operator for any column - just NA. If someone could help me to troubleshoot the issue, that would be great. And any other suggestions would also be helpful. I'm most comfortable with the tidyverse and base R and less comfortable with data.table.</p>
  <p>Below is a snippet of data for a reproducible example:</p>
  <pre><code>my_data &lt;- data.frame(&quot;student_id&quot; = 1:6, 
                        &quot;entry_date_1&quot; = as.Date(c(&quot;2017-11-09&quot;,&quot;2017-08-14&quot;,&quot;2017-08-14&quot;,&quot;2018-01-24&quot;,&quot;2017-10-04&quot;,&quot;2017-08-14&quot;)), 
                        &quot;withdrawal_date_1&quot; = as.Date(c(&quot;2018-05-24&quot;,&quot;2017-12-15&quot;,&quot;2018-06-01&quot;,&quot;2018-02-25&quot;,&quot;2017-11-11&quot;,&quot;2018-05-24&quot;)),
                        &quot;entry_date_2&quot; = as.Date(c(NA,&quot;2017-12-16&quot;,&quot;2018-01-16&quot;,&quot;2018-04-03&quot;,&quot;2017-12-12&quot;,NA)), 
                        &quot;withdrawal_date_2&quot; = as.Date(c(NA,&quot;2018-05-24&quot;,&quot;2018-03-20&quot;,&quot;2018-05-24&quot;,&quot;2018-05-24&quot;,NA)))
  </code></pre>
  <p>Thanks in advance for any help!</p>