I have two dataframes: 'Probes' and 'Events'. The below code will generate reproducible sample of these dataframes. Probes.subset is a dataframe of all observations from Probes that intersect the datetime ranges in Events. The code below will generate 6 events...in reality I have nearly 200 discreet events.
Aim: I need to join the Event.name from Events, to each corresponding observation in Probes.subset based on the Timestamp so that each observation is associated with the correct Event.
I have previously tried a FOR loop but this is incredibly slow and doesn't finish as my data is over 180,000 rows long. I have a feeling this can be solved by writing a function and using something like apply but I am a complete newbie to functions in R and none of the ones I've created work.
library(dplyr)
# Generate Probes data
start <- as.POSIXct("01/06/2016 01:00", format = "%d/%m/%Y %H:%M")
end <- start + as.difftime(1, units = "days")
Timestamp <- seq(from = start, to = end, by = "10 mins")
Value <- round(runif(145) * 100, 2)
Probes <- data.frame(Timestamp, Value)
# Generate Events data
Event.name <- seq(1, 6)
Event.start <- as.POSIXct(c("01/06/2016 01:20", "01/06/2016 05:00",
"01/06/2016 06:30", "01/06/2016 12:00",
"01/06/2016 17:40", "01/06/2016 19:20"),
format = "%d/%m/%Y %H:%M")
Event.end <- as.POSIXct(c("01/06/2016 02:00", "01/06/2016 05:30",
"01/06/2016 07:20", "01/06/2016 14:00",
"01/06/2016 18:10", "01/06/2016 21:40"),
format = "%d/%m/%Y %H:%M")
Events <- data.frame(Event.name, Event.start, Event.end)
# Subset probes data to fall within Events bounds
Probes.subset <- Probes %>%
mutate(InRange = Timestamp %in% unlist(Map(
`:`,
Events$Event.start,
Events$Event.end
))) %>%
filter(InRange == "TRUE")