Home > Software engineering >  group the data smoothed over 0.5 second means into 1 min intervals and obtain the range in r
group the data smoothed over 0.5 second means into 1 min intervals and obtain the range in r

Time:12-17

I have a data frame smoothed over 0.5 second means. I have to get the group the data into 1 min intervals and get the z_diff = z_max - z_min for those 1 min interval. All rows (120 rows) within 1 min period should have the same z_diff value.

I used the cut function as below. But it split the data into 1 min intervals. However I need to have the same z_diff value for all the rows within 1 min interval. How am I supposed to do it without splitting data in r

df_1min <- df_selected %>% group_by(Time_Interval = cut(df_selected$Timestamp, "60 sec")) %>% summarise(z_diff = max(z) - min(z))

sample data

Timestamp <- c("10:50:00.5", "10:50:01.0", "10:50:01.5", "10:50:02.0", "10:50:02.5", "10:50:03.0")

z <- c(1, 2, 3, 5, 8, 7)

CodePudding user response:

library(tidyverse)

Timestamp <- c("10:50:00.5", "10:50:01.0", "10:50:01.5", "10:50:02.0", "10:50:02.5", "10:50:03.0")

z <- c(1, 2, 3, 5, 8, 7)

tibble(z, Timestamp) %>% 
    mutate(Timestamp = lubridate::hms(Timestamp),
           Time_Interval = floor(Timestamp)) %>% 
    group_by(Time_Interval) %>% 
    mutate(z_diff = max(z) - min(z))
  • Related