0

I have data for temperature measurements every 15 minutes for a whole year. I've calculated the average temperature for these hours over the course of the year. What I wish to calculate though, is the average per hour rather than per 15 minutes.

What I currently calculated is:

    Hour    Average_Temperature 
 1  0:00:00 14.35748
 2  0:15:00 14.30943
 3  0:30:00 14.18519
 4  0:45:00 14.04781
 5  1:00:00 13.93074
 6  1:15:00 13.78855
 7  1:30:00 13.67138
 8  1:45:00 13.54646

I've achieved the first table using simply the package dplyr. I call group_by() on the Hour variable and then call in summarise().

AvgHr <- mydata %>% 
  group_by(Hour) %>% 
  summarise(Average_Temperature = mean(Temp))

What I do not know how to do is whether I can do a nested grouping where I group and then average per full hour rather than per individual quarter hour. In the table above I would then consider the values for observation 0:00:00, 0:15:00, 0:30:00, 0:45:00 under the same observation of 0:00:00.

What I would then get, would be the following:

    Hour    Average_Temperature
1   0:00:00 14.xxxxx
2   1:00:00 13.xxxxx
3   2:00:00 13.xxxxx
Leonardo
  • 2,439
  • 33
  • 17
  • 31

1 Answers1

2

Yes, just read the Hour as a period object instead of a string, extract the hour and then group and summarize:

library(tidyverse)
library(lubridate)

#> df <- tribble(
#> ~Hour, ~Average_Temperature, 
#> "0:00:00", 14.35748,
#> "0:15:00", 14.30943,
#> "0:30:00", 14.18519,
#> "0:45:00", 14.04781,
#> "1:00:00", 13.93074,
#> "1:15:00", 13.78855,
#> "1:30:00", 13.67138,
#> "1:45:00", 13.54646)

df %>% 
  mutate(Time = hms(Hour), Hour = hour(Time)) %>% 
  group_by(Hour) %>% 
  summarize(H_Average_Temp = mean(Average_Temperature ))
#> # A tibble: 2 x 2
#>    Hour H_Average_Temp
#>   <dbl>          <dbl>
#> 1     0           14.2
#> 2     1           13.7
c1au61o_HH
  • 867
  • 7
  • 14