2

I have a data.frame (df1) of times (POSIXct), hourly minimum temperatures (Tmin) and hourly maximum temperatures (Tmax). I also have a data.frame (df2) of daily sunrise and sunset times. I would like to calculate average Tmin and Tmax for every period of day and night. I am struggling, because the day changes at midnight. I would like the night periods to be considered as "belonging" to the previous day. An example of the output I am looking for is df3.

This my first time asking a question. I am happy to correct any formatting mistakes or provide more information. Thanks!

> df1 created from dput()
  structure(list(Time = structure(c(1514786400, 1514790000, 1514793600, 
1514797200, 1514800800, 1514804400, 1514808000, 1514811600, 1514815200, 
1514818800, 1514822400, 1514826000, 1514829600, 1514833200, 1514836800, 
1514840400, 1514844000, 1514847600, 1514851200, 1514854800, 1514858400, 
1514862000, 1514865600, 1514869200, 1514872800, 1514876400, 1514880000, 
1514883600, 1514887200, 1514890800, 1514894400, 1514898000, 1514901600, 
1514905200, 1514908800, 1514912400, 1514916000, 1514919600, 1514923200, 
1514926800, 1514930400, 1514934000, 1514937600, 1514941200, 1514944800, 
1514948400, 1514952000, 1514955600, 1514959200, 1514962800, 1514966400, 
1514970000, 1514973600, 1514977200, 1514980800, 1514984400, 1514988000, 
1514991600, 1514995200), class = c("POSIXct", "POSIXt"), tzone = ""), 
    Tmin = c(17.38, 16.41, 15.7, 14.55, 13.7, 13.22, 12.01, 11.23, 
    11.17, 11.47, 12.19, 14.37, 15.5, 16.41, 17.73, 18.69, 19.11, 
    18.09, 16.95, 16.24, 15.58, 14.97, 14.55, 14.31, 13.7, 13.16, 
    13.04, 12.8, 13.28, 13.16, 12.67, 12.43, 12.43, 12.86, 13.52, 
    14.36, 16.17, 17.37, 19.48, 20.87, 21.23, 20.27, 19.25, 19.19, 
    18.95, 18.95, 18.95, 18.47, 17.62, 16.6, 16.43, 16.13, 16.67, 
    17.86, 17.32, 16.79, 16.79, 21.8, 22.16), Tmax = c(18.89, 
    17.56, 16.53, 15.94, 14.61, 13.88, 13.28, 12.19, 11.77, 12.43, 
    14.73, 15.86, 16.83, 18.03, 19.29, 19.6, 19.96, 19.35, 18.09, 
    16.95, 16.31, 15.7, 15.1, 14.85, 14.37, 13.82, 13.34, 13.4, 
    13.76, 13.82, 13.28, 12.86, 13.22, 13.7, 14.55, 16.05, 17.74, 
    19.96, 20.99, 21.71, 21.96, 21.77, 20.27, 19.62, 19.37, 19.25, 
    19.19, 19.19, 18.47, 17.56, 17.15, 17.03, 18.73, 18.91, 18.05, 
    17.75, 19.27, 21.98, 25.29)), row.names = 3:61, class = "data.frame")

> df2
    structure(list(Rise = structure(c(1514811657, 1514898068, 1514984477
), class = c("POSIXct", "POSIXt"), tzone = ""), Set = structure(c(1514847274, 
1514933719, 1515020165), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA, 
3L), class = "data.frame")

> df3
  Day         Day_Tmin    Day_Tmax    Night_Tmin    Night_Tmax
  2018-01-01  <num>       <num>       <num>         <num>
Texanum
  • 23
  • 4
  • Is everything prior to sunrise considered to belong to the day before? So 2018-01-01's night ends at 2018-01-02 07:01:08? – Martin Gal Jun 01 '20 at 21:59
  • Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Jun 01 '20 at 22:00
  • @MartinGal yes, that is correct. Every night period between sunset and sunrise would belong to the day during which the night period began. The splitting of night periods by midnight has made it difficult for me to apply something like `aggregate()`. – Texanum Jun 02 '20 at 13:39
  • @Tung I edited my post and added a more complete set of hourly data covering two full days. The three rows of `df2` apply to those days. I don't have any code worth mentioning that I have run, because I can't really get my head around how to begin this problem. Thus there are no errors, etc. that I have encountered. Does that help? – Texanum Jun 02 '20 at 13:51
  • @Tung I was able to recreate the hourly data using `dput()` (I think). I'm trying to follow the reprex guidelines, but this is quite new to me and difficult. Please criticize as necessary. Thanks! – Texanum Jun 02 '20 at 14:40

0 Answers0