title: Filter Timeseries Data Using Sunrise and Sunset Times
body: <p>I'm trying to subset data that falls between sunset and sunrise.
  The data:</p>
  <pre><code>library(tidyverse)
  library(lubridate)
  library(suncalc)
  
  dat &lt;- tibble(datetime = seq(as.POSIXct('2020-08-03 00:00:00'), 
                               as.POSIXct('2020-08-09 12:00:00'), 
                               by=3600),
                var1 = rnorm(157,2,1),
                var2 = rnorm(157,3,5)) %&gt;% 
    mutate(getSunlightTimes(date = as.Date(datetime, format = '%m/%d/%Y'),
                           lat = 43.1, lon = -76.2, tz = 'America/New_York',
                           keep = c('sunrise', 'sunset'))) %&gt;% 
    select(c(datetime, var1, var2, sunrise, sunset)) 
  </code></pre>
  <p>Then I would like to subset the data so that I only retain rows where <code>datetime</code> falls between sunrise and sunset on a given day. I tried:</p>
  <pre><code>myrange &lt;- as.interval(unique(dat$sunrise), unique(dat$sunset))
  
  dat &lt;- dat %&gt;% 
    filter(datetime %within% myrange)
  </code></pre>
  <p>This compiles but throws warnings and does not include all the rows of data that it should. Thanks in advance.</p>