Create mean between two julian days for multiple years

I want to calculate the mean of the minimum temperature value (minTemp) between April 1st and April 5th for each year of a data set (montbrun).

I have tried simply filter(julianDays >= 091 & julianDays <= 095), but as I was expecting, it doesn't work cause it takes only the first values of the data set that fits to 091-095 values (in julian days).

In other words, my goal is to calculate mean, for every year of the data set between april (Month=4) 1st and 5th (Days= 1 to 5), but in julian days. What I don't know is how to code this when I have multiple years, months and all the julian days in a single year. Just to clarify, on a normal year, January 1st is 001 and December 31st is 365 in julian days.

Any idea how to code this?

My Data

montbrun <- structure(list(Year = c(2005, 2005, 2005, 2005, 2005, 2006, 2006,
2006, 2006, 2006, 2007, 2007, 2007, 2007, 2007, 2008, 2008, 2008,
2008, 2008), Month = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4), Day = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
1, 2, 3, 4, 5), Date/Time = structure(c(1112313600, 1112400000,
1112486400, 1112572800, 1112659200, 1143849600, 1143936000,
1144022400, 1144108800, 1144195200, 1175385600, 1175472000,
1175558400, 1175644800, 1175731200, 1207008000, 1207094400,
1207180800, 1207267200, 1207353600), class = c("POSIXct", "POSIXt"),
tzone = "UTC"),
julianDays = c("091", "092", "093", "094", "095", "091",
"092", "093", "094", "095", "091", "092", "093", "094", "095",
"092", "093", "094", "095", "096"), minTemp = c(1, -5, -2,
-2, -6, 1, -9, 0, -7.5, -5, -7, 0.5, -2.5, -1.5, -10, -8,
-17.5, -8, -6.5, -5)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))

My Code

example <- montbrun %>%
             group_by(Year) %>%
               filter(Month == 4) %>%
                 filter(Day >= 1 & Day <= 5) %>%
                   summarise(mintemp = mean(minTemp))

If someone could help me with that, it would be fantastic !
Thanks !

Here are two ways to make the calculation based on the julianDays column. Notice that it differs from the calculation based on April 1 - April 5 in 2008 because the leap day shifts the julianDays value in April. Does this solve your problem?

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
montbrun <- structure(list(Year = c(2005, 2005, 2005, 2005, 2005, 2006, 2006,
                                    2006, 2006, 2006, 2007, 2007, 2007, 2007, 2007, 2008, 2008, 2008,
                                    2008, 2008), 
                           Month = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
                                     4, 4, 4, 4, 4), 
                           Day = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5,
                                   1, 2, 3, 4, 5), 
                           DateTime = structure(c(1112313600, 1112400000,
                                                   1112486400, 1112572800, 1112659200, 1143849600, 1143936000,
                                                   1144022400, 1144108800, 1144195200, 1175385600, 1175472000,
                                                   1175558400, 1175644800, 1175731200, 1207008000, 1207094400,
                                                   1207180800, 1207267200, 1207353600), class = c("POSIXct", "POSIXt"),tzone = "UTC"),
                           julianDays = c("091", "092", "093", "094", "095", "091",
                                          "092", "093", "094", "095", "091", "092", "093", "094", "095",
                                          "092", "093", "094", "095", "096"), 
                           minTemp = c(1, -5, -2,
                                       -2, -6, 1, -9, 0, -7.5, -5, -7, 0.5, -2.5, -1.5, -10, -8,
                                       -17.5, -8, -6.5, -5)), 
                      row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))

montbrun %>%
  group_by(Year) %>%
  filter(Month == 4) %>%
  filter(Day >= 1 & Day <= 5) %>%
  summarise(mintemp = mean(minTemp))
#> # A tibble: 4 x 2
#>    Year mintemp
#>   <dbl>   <dbl>
#> 1  2005    -2.8
#> 2  2006    -4.1
#> 3  2007    -4.1
#> 4  2008    -9


montbrun %>%
  group_by(Year) %>%
  filter(julianDays >="091", julianDays <= "095") %>%
  summarise(mintemp = mean(minTemp))
#> # A tibble: 4 x 2
#>    Year mintemp
#>   <dbl>   <dbl>
#> 1  2005    -2.8
#> 2  2006    -4.1
#> 3  2007    -4.1
#> 4  2008   -10

montbrun %>%
  mutate(julianDays = as.numeric(julianDays)) %>%
  group_by(Year) %>%
  filter(julianDays >= 91, julianDays <= 95) %>%
  summarise(mintemp = mean(minTemp))
#> # A tibble: 4 x 2
#>    Year mintemp
#>   <dbl>   <dbl>
#> 1  2005    -2.8
#> 2  2006    -4.1
#> 3  2007    -4.1
#> 4  2008   -10

Created on 2022-07-24 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.