Summarizing time series data

A screenshot is not very useful for people trying to help you since we cant easily copy from that, so I'm going to give a general example of how you can aggregate data by custom time intervals independently of the number of rows that fall into each interval.

library(tidyverse)
library(tibbletime)

# Made up data since you haven't provided sample data in a copy/paste friendly format
sample_df <- data.frame(
    date_time = as.POSIXct(c("2021-08-01 00:02:03 UTC", "2021-08-01 00:14:03 UTC", 
                              "2021-08-01 00:35:03 UTC", "2021-08-01 00:47:03 UTC")),
    other_variable_1 = rnorm(4),
    other_variable_2 = rnorm(4)
)

# Example solution
sample_df %>%
    as_tbl_time(date_time) %>% 
    collapse_by("30 min", side = "start", clean = TRUE) %>% 
    group_by(date_time) %>% 
    summarise(across(.fns = last))
#> # A time tibble: 2 × 3
#> # Index: date_time
#>   date_time           other_variable_1 other_variable_2
#>   <dttm>                         <dbl>            <dbl>
#> 1 2021-08-01 00:00:00            0.941            1.43 
#> 2 2021-08-01 00:30:00            0.778            0.834

Created on 2021-08-01 by the reprex package (v2.0.0)

If you need more specific help, our an alternative solution, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like