-1

I have a datafile containing ~60,000 observations from 70 individuals. The datafile looks like this: datafile exampledatafile

I wish to select the last 5 minutes of data for each individual. Each individual has a different number of observations. Is there a way to identify the last observation for each individual and select the preceding 5 minutes of data? I used the code below to identify the first 5 minutes but I am unsure how to do the same for the last 5 minutes.

#Set date and time format
df$DateTime=paste(df$Date, df$Time)
df$DateTime <- as.POSIXct(df$DateTime, format="%d/%m/%Y %H:%M:%S")

df$ID <- as.numeric(as.character(df$ID)) 
df$Value <- as.numeric(as.character(df$Value)) 


extract=df %>%
  group_by(ID, DateTime = cut(DateTime, breaks="5 min")) %>%
  summarize(Value=median(Value))

Thanks in advance!

1 Answers1

0

This should filter to the last 5 minutes of observations per individual.

df %>%
  group_by(ID) %>%
  mutate(last_time = max(DateTime)) %>%
  ungroup() %>%
  filter(DateTime >= last_time - 5*60)
Jon Spring
  • 55,165
  • 4
  • 35
  • 53