简体   繁体   English

在 R 中,如何自动检测首次出现非标准时差的位置?

[英]In R, how can I automate the detection of where a non-standard time difference first occurs?

I have a data frame where each row is a different timestamp.我有一个数据框,其中每一行都是不同的时间戳。 The older data in the data frame is collected at 30-minute intervals while the more recent data is collected at 15-minute intervals.数据框中较旧的数据每隔 30 分钟收集一次,而较新的数据每隔 15 分钟收集一次。 I would like to run a for loop (or maybe an ifelse statement) that calulates the time difference between each row, if the difference is equal to 30 minutes (below example uses 1800 seconds) then the loop continues, but if the loop encounters a 15 minute time difference (below example uses 900 seconds) it stops and tells me which row this first occured on.我想运行一个计算每行之间的时间差的 for 循环(或者可能是 ifelse 语句),如果差值等于 30 分钟(下面的示例使用 1800 秒),那么循环继续,但如果循环遇到15 分钟的时差(下面的示例使用 900 秒)它会停止并告诉我这首先发生在哪一行。

x <- as.POSIXct("2000-01-01 01:00", tz = "", "%Y-%m-%d %H:%M")
y <- as.POSIXct("2000-01-10 12:30", tz = "", "%Y-%m-%d %H:%M")
xx <- as.POSIXct("2000-01-10 12:45", tz = "", "%Y-%m-%d %H:%M")
yy <- as.POSIXct("2000-01-20 23:45", tz = "", "%Y-%m-%d %H:%M")

a.30 <- as.data.frame(seq(from = x, to = y, by = 1800))
names(a.30)[1] <- "TimeStamp"
a.15 <- as.data.frame(seq(from = xx, to = yy, by = 900))
names(a.15)[1] <- "TimeStamp"

dat <- rbind(a.30,a.15)

In the example dat data frame, the time difference switches from 30 minute to 15 minute intervals at row 457. I would like to automate the process of identifing the row where this change in time difference first occurs.在示例dat数据帧中,时差在第 457 行从 30 分钟切换到 15 分钟。我想自动识别第一次发生这种时差变化的行的过程。

We can use difftime to calculate the difference in time in mins and create a logical vector based on the difference我们可以使用difftime来计算以mins为单位的时间差异,并根据差异创建一个逻辑向量

library(dplyr)
dat %>%
   summarise(ind = which.max(abs(as.numeric(difftime(TimeStamp, 
       lag(TimeStamp, default = TimeStamp[2]), unit = 'min'))) < 30))
#  ind
#1 457

Here's another way that uses slightly different logic.这是另一种使用稍有不同逻辑的方法。 Calculate the difference, and create a column with the row number.计算差异,并使用行号创建一列。 Then filter to where the difference is 15, and take the first row.然后过滤到差值为 15 的位置,并取第一行。

library(tidyverse)

dat %>% mutate(Diff = TimeStamp - lag(TimeStamp), rownum = row_number()) %>% 
  filter(Diff == 15) %>% 
  slice(1)

            TimeStamp    Diff rownum
1 2000-01-10 12:45:00 15 mins    457

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM