Create mean temperature between two dates for several years

RStudio Version 1.4.1717.Macintosh; Intel Mac OS X 12_4_0

Hi,
I have a data set with dates in April for 5 years. A minimum temperature is associated to each date. I want to calculate the mean temperature in a set interval of 5 days (from April 1st to April 5th), for each year. E.g. in 2005 the mean minum temperature was 3 celsius, in 2006 5 celsius, etc.

Here's my data set:
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"))

I tried running this function, but got clue less at the end because I didn't know what to return my function to:
myfunc <- function(x){
quinzeJours<- seq(as.Date("2005-04-01"), as.Date("2005-04-15"), by="days")
min <- montbrun[montbrun$Date/Time < x &montbrun$ >= quinzeJours , ]
avg_temp <- mean(montbrun$Min Temp (°C))
tempmin <- data.frame(dob = x, avg_temp = avg_temp)
return(dat)
}

In summary, I need help for creating a function that would allow me to create a mean of temperature of a set interval for many years of climate data.

Thanks !

Hi @Chris6698 ,
Does this code do what you require? I have used {dplyr} rather than a used-defined function.


indata <- 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"))

indata

library(dplyr)

indata %>%
  group_by(Year) %>%
  filter(Month == 4) %>%
  filter(Day >= 1 & Day <= 5) %>%
  summarise(mean_min = mean(minTemp))

Produces:


# A tibble: 4 × 2
   Year mean_min
      
1  2005     -2.8
2  2006     -4.1
3  2007     -4.1
4  2008     -9  

Wow ! Thank you so much that works perfectly!

This topic was automatically closed 7 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.