Search code examples
rdatetimemergedate-sunrise

Merge 2 timestamp data sets and create day/night column and dusk/dawn column in R


I have 2 data sets, one contains my data and the other contains the sunrise/sunset data from the NAVY Observatory.

head(tagdata)
  X    ID                 ex2  meanlat  meanlong meanEast meanNorth meandepth rec numhits           species depthcat       Date     Time
1 1 34931 2015-07-31 13:06:26 18.34127 -64.98051 290711.7   2029082        NA  10      42 Lutjanus synagris       NA 2015-07-31 13:06:26
2 2 34931 2015-07-31 14:06:26 18.34141 -64.98054 290708.6   2029097        NA  10      55 Lutjanus synagris       NA 2015-07-31 14:06:26
3 3 34931 2015-07-31 15:06:26 18.34115 -64.98023 290741.3   2029068        NA   9      53 Lutjanus synagris       NA 2015-07-31 15:06:26
4 4 34931 2015-07-31 16:06:26 18.34158 -64.98012 290753.0   2029115        NA   9      38 Lutjanus synagris       NA 2015-07-31 16:06:26
5 5 34931 2015-07-31 17:06:26 18.34162 -64.97979 290788.0   2029119        NA   8      32 Lutjanus synagris       NA 2015-07-31 17:06:26
6 6 34931 2015-07-31 18:06:26 18.34102 -64.97949 290818.5   2029053        NA   6      27 Lutjanus synagris       NA 2015-07-31 18:06:26

head(AstroSun)
        Date Srise Sset        Sunrise_date         Sunset_date Astro_Begin Astro_End         ABegin_date           AEnd_date
1 2015-01-01   652 1755 2015-01-01 06:52:00 2015-01-01 17:55:00         534      1913 2015-01-01 05:34:00 2015-01-01 19:13:00
2 2015-01-02   652 1756 2015-01-02 06:52:00 2015-01-02 17:56:00         534      1913 2015-01-02 05:34:00 2015-01-02 19:13:00
3 2015-01-03   652 1756 2015-01-03 06:52:00 2015-01-03 17:56:00         535      1914 2015-01-03 05:35:00 2015-01-03 19:14:00
4 2015-01-04   653 1757 2015-01-04 06:53:00 2015-01-04 17:57:00         535      1914 2015-01-04 05:35:00 2015-01-04 19:14:00
5 2015-01-05   653 1757 2015-01-05 06:53:00 2015-01-05 17:57:00         535      1915 2015-01-05 05:35:00 2015-01-05 19:15:00
6 2015-01-06   653 1758 2015-01-06 06:53:00 2015-01-06 17:58:00         536      1915 2015-01-06 05:36:00 2015-01-06 19:15:00

I want to merge both data sets and create a new column that adds day or night for each observation, based on the sunrise and sunset time frames from the Navy data. How do I go about it? Also, how can I add dusk and dawn to each observation, where by each observation starts an hour before sunrise or sunset and ending an hour after sunrise or sunset time?


Solution

  • Because daylight hours are always* contained within one calendar day, I think the best thing to do is merge your two data sets by Date and then create an interval of daylight hours for each day, then test if the tag date is within this interval.

    I would do this using %within% along with lubridate::interval().

    For your data, I will act as if all the dates are currently just character strings.

    library(dplyr)
    library(lubridate)
    
    tagdata1 <- left_join(tagdata, AstroSun, by = "Date") %>%
      mutate(Sunrise_date = ymd_hms(Sunrise_date)) %>%
      mutate(Sunset_date = ymd_hms(Sunset_date)) %>%
      mutate(tag_datim = ymd_hms(paste0(Date, Time))) %>%
      mutate(daylight_intvl = interval(Sunrise_date, Sunset_date)) %>%
      mutate(during_day = tag_datim %within% daylight_intvl)
    

    tagdata1$during_day is a logical TRUE for a tag date/time that is during daylight hours.

    You should be able to figure out dusk and dawn by using the above commands as well as + dhours(1) which adds an hour to any date/time value.

    *I haven't considered any exotic Arctic circle edge cases or different timezones etc.