I despair of this seemingly easy task: I have a number vector values
and a time vector time
and I need to find out for how long the values are inside a certain range. Here is some data:
df <- data.frame(time= c(1,3:10), values= c(7,3:10))
For this data the values were for 4.5 hours outside the range 3.5 to 6.5. Here a visualization how to determine those 4.5 hours:
In this plot the x axis is the time, the y axis the values, the points the real measures, the dashed line are the range borders 3.5 and 6.5, the full line is just a help to better see when the range borders are crossed.
Am I missing an obvious way to determine for how long the values are outside the range?
The plot is created with
threshold_low <- 3.5
threshold_high <- 6.5
ggplot(data= df, mapping= aes(time, values)) +
geom_point() +
geom_line() +
geom_hline(yintercept= c(threshold_low, threshold_high), linetype= "dashed") +
scale_x_continuous(breaks=seq(0, 10, 1)) +
scale_y_continuous(breaks=seq(0, 10, 1))