title: Unexpected dplyr::right_join() behavior for expanding POSIXct time series
body: <p>I have a data frame containing some daily data timestamped at midnight on each day and some hourly data timestamped at the beginning of each hour throughout the day. I want to expand the data so it's all hourly, and I'd like to do so within a tidyverse "pipe chain". </p>
  
  <p>My thought was to create a data frame containing the full hourly time series and then <code>dplyr::right_join()</code> my data against this time series. I thought this would populate the proper values where there was a match for the daily data (at midnight) and populate <code>NA</code> wherever there was no match (any hour except midnight). This seems to work only when the time series in my data is daily only, rather than a mix of daily and hourly values, which was unexpected. Why does the right join not expand the daily time series when it coexists in a data frame along with another hourly time series?</p>
  
  <p>I've generated a minimal example below. My representative data set that I want to expand is named <code>allData</code> and contains a mix of daily and hourly datasets from two different time series variables, <code>Daily TS</code> and <code>Hourly TS</code>.</p>
  
  <pre><code>dailyData &lt;- data.frame( 
    DateTime = seq.POSIXt(lubridate::ymd_hms('2019-01-01', truncated=3), 
                          lubridate::ymd_hms('2019-01-07', truncated=3), 
                          by='day'),
    Name = 'Daily TS'
    )
  
  allHours &lt;- data.frame(
    DateTime = seq.POSIXt(lubridate::ymd_hms('2019-01-01', truncated=3), 
                          lubridate::ymd_hms('2019-01-07 23:00:00'), 
                          by='hour')
  )
  
  hourlyData &lt;- allHours %&gt;%
    dplyr::mutate( Name = 'Hourly TS' )
  
  allData &lt;- rbind( dailyData, hourlyData )
  </code></pre>
  
  <p>This gives</p>
  
  <pre><code>head( allData, n=15 )
                DateTime      Name
  1  2019-01-01 00:00:00  Daily TS
  2  2019-01-02 00:00:00  Daily TS
  3  2019-01-03 00:00:00  Daily TS
  4  2019-01-04 00:00:00  Daily TS
  5  2019-01-05 00:00:00  Daily TS
  6  2019-01-06 00:00:00  Daily TS
  7  2019-01-07 00:00:00  Daily TS
  8  2019-01-01 00:00:00 Hourly TS
  9  2019-01-01 01:00:00 Hourly TS
  10 2019-01-01 02:00:00 Hourly TS
  11 2019-01-01 03:00:00 Hourly TS
  12 2019-01-01 04:00:00 Hourly TS
  13 2019-01-01 05:00:00 Hourly TS
  14 2019-01-01 06:00:00 Hourly TS
  15 2019-01-01 07:00:00 Hourly TS
  </code></pre>
  
  <p>Now, I thought that <code>dplyr::right_join()</code> of the full hourly sequence of <code>POSIXct</code> values against <code>allData$DateTime</code> would have expanded the daily time series, leaving NA values for any hours not explicitly present in the data. I could then use <code>tidyr::fill()</code> to fill these in over the day. However, the following code does not behave this way:</p>
  
  <pre><code>expanded_BAD &lt;- allData %&gt;%
    dplyr::right_join( allHours, by='DateTime' ) %&gt;%
    tidyr::fill( dplyr::everything(), .direction='down' ) %&gt;%
    dplyr::arrange( Name, DateTime )
  </code></pre>
  
  <p><code>expanded_BAD</code> shows that the daily data hasn't been expanded by the <code>right_join()</code>. That is, the hours in <code>allHours</code> missing from <code>allData</code> were not retained in the result, which I thought was the whole purpose of using a right join. Here's the head of the result:</p>
  
  <pre><code>head(expanded_BAD, n=15)
                DateTime      Name
  1  2019-01-01 00:00:00  Daily TS
  2  2019-01-02 00:00:00  Daily TS
  3  2019-01-03 00:00:00  Daily TS
  4  2019-01-04 00:00:00  Daily TS
  5  2019-01-05 00:00:00  Daily TS
  6  2019-01-06 00:00:00  Daily TS
  7  2019-01-07 00:00:00  Daily TS
  8  2019-01-01 00:00:00 Hourly TS
  9  2019-01-01 01:00:00 Hourly TS
  10 2019-01-01 02:00:00 Hourly TS
  11 2019-01-01 03:00:00 Hourly TS
  12 2019-01-01 04:00:00 Hourly TS
  13 2019-01-01 05:00:00 Hourly TS
  14 2019-01-01 06:00:00 Hourly TS
  15 2019-01-01 07:00:00 Hourly TS
  </code></pre>
  
  <p>Interestingly, if we perform the exact same right join on <em>only</em> the daily data, we get the desired result:</p>
  
  <pre><code>dailyData_expanded_GOOD &lt;- dailyData %&gt;%
    dplyr::right_join( allHours, by='DateTime' ) %&gt;%
    tidyr::fill( dplyr::everything(), .direction='down' )
  </code></pre>
  
  <p>Here's the head:</p>
  
  <pre><code>head(dailyData_expanded_GOOD, n=15)
                DateTime    Value
  1  2019-01-01 00:00:00 Daily TS
  2  2019-01-01 01:00:00 Daily TS
  3  2019-01-01 02:00:00 Daily TS
  4  2019-01-01 03:00:00 Daily TS
  5  2019-01-01 04:00:00 Daily TS
  6  2019-01-01 05:00:00 Daily TS
  7  2019-01-01 06:00:00 Daily TS
  8  2019-01-01 07:00:00 Daily TS
  9  2019-01-01 08:00:00 Daily TS
  10 2019-01-01 09:00:00 Daily TS
  11 2019-01-01 10:00:00 Daily TS
  12 2019-01-01 11:00:00 Daily TS
  13 2019-01-01 12:00:00 Daily TS
  14 2019-01-01 13:00:00 Daily TS
  15 2019-01-01 14:00:00 Daily TS
  </code></pre>
  
  <p>Why does the right join do different things on the full data compared to only the daily data?</p>