I have a data frame with systolic blood pressure and date/time. The readings were sometimes taken within minutes of each other, sometimes not. An example is below:
library(tidyverse)
date_time <- c("Jan 29 2020 13:46:08" ,
"Jan 29 2020 13:42:53" ,
"Jan 29 2020 12:13:27" ,
"Jan 29 2020 12:11:19" ,
"Jan 29 2020 12:09:21" ,
"Jan 28 2020 12:22:26" ,
"Jan 27 2020 8:22:20")
systol <- c(132 , 132 , 118 , 115 , 110 , 148 , 120)
df <- data.frame(date_time , systol) %>%
mutate(dtetime = lubridate::mdy_hms(date_time)
I want to group the systolic readings taken near to each other in time--arbitrarily, with the last reading taken within 10 minutes of the first reading in any group--and average the readings (and ideally average the date/time of the reading as well). I've tried thinking it through using lag and lead functions and then group_by, and could not devise a way to do that if there are not 2 readings in a group. I'm most familiar with the tidyverse so prefer that approach but am interested in any way of doing this. I'm relatively new to R. Thank you for any help!