Search code examples
rerror-handlingaverage

How do I get a daily average for data points generated every 15 minutes?


This is my first question and I'm also a very new learner to r so please bear with me. I am working on some gauge data from about 4 months and it took temperature readings every 15 minutes. I am wanting to get a daily average from this data and then graph it. I have looked some stuff up and tried to run what people have suggested, but keep running into this error:

library(lubridate)
library(dplyr)

DownGauge %>% 
+   group_by(day = day(date)) %>%
+   summarise(mean_Temperature = mean(Temperature))
**Error in `group_by()`:
ℹ In argument: `day = day(date)`.
Caused by error in `as.POSIXlt.default()`:
! do not know how to convert 'x' to class “POSIXlt”**

Any help would be much appreciated. I am really struggling to find a good answer to this question. I have included the dput and head of my data below because I saw that is good policy to provide. Let me know if anything else is needed. Thanks.

dput(head(DownGauge))
structure(list(Date = structure(c(1709770500, 1709771400, 1709772300, 
1709773200, 1709774100, 1709775000), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Temperature = c(8, 7.9, 7.9, 7.9, 7.9, 7.9)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
head(DownGauge)
# A tibble: 6 × 2
  Date                Temperature
  <dttm>                    <dbl>
1 2024-03-07 00:15:00         8  
2 2024-03-07 00:30:00         7.9
3 2024-03-07 00:45:00         7.9
4 2024-03-07 01:00:00         7.9
5 2024-03-07 01:15:00         7.9
6 2024-03-07 01:30:00         7.9`

I tried to change the as.POSIXlt to as.POSIXct cause I thought that would fix it but when I created a new data frame with the changed date format, it just switched automatically back to as.POSIXlt. Not sure why.


Solution

  • You are interested in grouping the date. ie if you group by day(date) then 1st of every month would be grouped together and thats incorrect. Consider using the following:

    DownGauge %>%
       group_by(Date = as.Date(Date)) %>%
       summarise(mean_temp = mean(Temperature))
    
    # A tibble: 1 × 2
      Date        mean_temp
      <date>          <dbl>
    1 2024-03-07       7.92