I have a time series on which two events (x and y) occur irregularly. I'm trying to write a bit of script that describes the time from x to the closest y event.
For example:
Time x y
11:01:00 1 0
11:03:04 0 1
11:03:34 0 0
11:06:12 1 1
11:12:00 0 0
I'm trying to create a vector where each row is the time from each '1' in y to the nearest '1' in the x vector.
So the above would return:
diff
02:04 (closest point is the previous row here)
0 (occurred on the same row so time difference is 0)
05:48 (occurred on the previous row)
reproducible example:
time<-c("11:01:00","11:03:04","11:03:34","11:06:12","11:12:00")
x<-c(1,0,0,1,0)
y<-c(0,1,0,1,0)
df<-data.frame(time,x,y)
I'm not really sure how to go about this and any help would be appreciated!
We can do in a couple of steps:
First by converting your data into POSIXct format so we can use arithmatic on the time column
df$time <- as.POSIXct(df$time, format = "%H:%M:%S")
Then we create two new columns, with the index of x where x is 1. I'm assuming your data is in time order here.
df$nextx <- ifelse(df$x == 1, which(df$x == 1), NA)
df$prevx <- rev(ifelse(df$x == 1, which(df$x == 1), NA))
By using tidyr::fill, we fill in the xx, to get the next and previous x for each y:
library(tidyr)
df <- df %>% fill(nextx, rev(prevx))
Then we use pmin to find the minimum distance at each row:
x = pmin(abs(df$time - df$time[df$nextx]), abs(df$time - df$time[df$prevx]))
and subset by the rows that have ys:
x[df$y == 1]
Time differences in secs
[1] 124 0
(presumably, you wanted your data to have a 1 for y in the last place, in which case we would get your desired answer):
Time differences in secs
[1] 124 0 348
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.