[英]Difference between nearest times in time two time series
I have a time series on which two events (x and y) occur irregularly. 我有一个时间序列,两个事件(x和y)不规则地发生。 I'm trying to write a bit of script that describes the time from x to the closest y event.
我正在尝试编写一些描述从x到最近y事件的时间的脚本。
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. 我正在尝试创建一个向量,其中每一行是从y中的每个'1'到x向量中最接近的'1'的时间。
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 首先,将您的数据转换为POSIXct格式,以便我们可以在时间列上使用算术
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. 然后,我们创建两个新列,索引为x,其中x为1。我假设您的数据按时间顺序排列。
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: 通过使用tidyr :: fill,我们填写xx,以获得每个y的下一个和上一个x:
library(tidyr)
df <- df %>% fill(nextx, rev(prevx))
Then we use pmin to find the minimum distance at each row: 然后,我们使用pmin查找每一行的最小距离:
x = pmin(abs(df$time - df$time[df$nextx]), abs(df$time - df$time[df$prevx]))
and subset by the rows that have ys: 和具有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): (大概是,您希望您的数据的y的最后一位为1,在这种情况下,我们将得到您想要的答案):
Time differences in secs
[1] 124 0 348
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.