创建一个值以显示在R中的事件的时间。

时间:2022-06-27 00:17:58

I am new to R so need a small help with the problem I am trying to code. Below is a dataset where I need to calculate days since alarms. I have time series data where the values are captured for each second and there is an alarm which happened in the 10th second. I want to the calculate the difference between time from 10th second to 1st sec and populate it in a separate column in hours. Once the value against the first alarm is 0 i.e. difference is zero, the next row should consider the next alarm and populate the days since alarm .Below is sample data

我对R很陌生,所以需要一个小的帮助来解决我正在尝试编码的问题。下面是一个数据集,在这里我需要计算自警报以来的天数。我有时间序列数据,这些值每秒钟都被捕捉到,并且在第10秒发生了一个警报。我想计算一下时间从10秒到1秒之间的时间差,然后在数小时内将其填充到单独的列中。一旦对第一个警报的值为0,即差值为0,则下一行应该考虑下一个警报,并填充自警报后的天数。下面是示例数据。

datetime         alarm
1/1/2015 0:00     NO
1/1/2015 0:01     NO
1/1/2015 0:02     NO 
1/1/2015 0:03     NO
1/1/2015 0:04     NO 
1/1/2015 0:05     NO
1/1/2015 0:06     NO
1/1/2015 0:07     NO
1/1/2015 0:08     NO
1/1/2015 0:09     NO
1/1/2015 0:10     YES
1/1/2015 0:11     NO
1/1/2015 0:12     NO
1/1/2015 0:13     NO
1/1/2015 0:14     YES
1/1/2015 0:15     NO
1/1/2015 0:16     NO
1/1/2015 0:17     NO

Thanks.

谢谢。

2 个解决方案

#1


0  

DF<-read.table(text="datetime,alarm
1/1/2015 0:00,NO
1/1/2015 0:01,NO
1/1/2015 0:02,NO
1/1/2015 0:03,NO
1/1/2015 0:04,NO
1/1/2015 0:05,NO
1/1/2015 0:06,NO
1/1/2015 0:07,NO
1/1/2015 0:08,NO
1/1/2015 0:09,NO
1/1/2015 0:10,YES
1/1/2015 0:11,NO
1/1/2015 0:12,NO
1/1/2015 0:13,NO
1/1/2015 0:14,YES
1/1/2015 0:15,NO
1/1/2015 0:16,NO
1/1/2015 0:17,NO",header=TRUE,sep=",")

library(data.table)
setDT(DF)
#create grouping variable
DF[, period := rev(cumsum(rev(alarm == "YES")))]

DF[, datetime := as.POSIXct(datetime, "%m/%d/%Y %H:%M", tz = "UTC")]

#split-apply-combine operation
Res <- DF[, .(start = min(datetime),
       end = max(datetime)),
   by = period]

#possibly you need to add a minute here, don't know from your description    
Res[, length := difftime(end, start, unit = "hour")]

Res[, period := max(period) - period + 1]
#   period               start                 end           length
#1:      1 2015-01-01 00:00:00 2015-01-01 00:10:00 0.16666667 hours
#2:      2 2015-01-01 00:11:00 2015-01-01 00:14:00 0.05000000 hours
#3:      3 2015-01-01 00:15:00 2015-01-01 00:17:00 0.03333333 hours

#2


0  

df <- data.frame(datetime=c('1/1/2015 0:00','1/1/2015 0:01','1/1/2015 0:02','1/1/2015 0:03','1/1/2015 0:04','1/1/2015 0:05','1/1/2015 0:06','1/1/2015 0:07','1/1/2015 0:08','1/1/2015 0:09','1/1/2015 0:10','1/1/2015 0:11','1/1/2015 0:12','1/1/2015 0:13','1/1/2015 0:14','1/1/2015 0:15','1/1/2015 0:16','1/1/2015 0:17'),alarm=c('NO','NO','NO','NO','NO','NO','NO','NO','NO','NO','YES','NO','NO','NO','YES','NO','NO','NO'),stringsAsFactors=F);
df$datetime <- as.POSIXct(df$datetime,'%d/%m/%Y %H:%M',tz='UTC');
w <- which(df$alarm=='YES');
df$diff <- difftime(df$datetime,df$datetime[c(1L,w)[findInterval(seq_along(df$alarm),w)+1L]],units='hours');
df;
##               datetime alarm             diff
## 1  2015-01-01 00:00:00    NO 0.00000000 hours
## 2  2015-01-01 00:01:00    NO 0.01666667 hours
## 3  2015-01-01 00:02:00    NO 0.03333333 hours
## 4  2015-01-01 00:03:00    NO 0.05000000 hours
## 5  2015-01-01 00:04:00    NO 0.06666667 hours
## 6  2015-01-01 00:05:00    NO 0.08333333 hours
## 7  2015-01-01 00:06:00    NO 0.10000000 hours
## 8  2015-01-01 00:07:00    NO 0.11666667 hours
## 9  2015-01-01 00:08:00    NO 0.13333333 hours
## 10 2015-01-01 00:09:00    NO 0.15000000 hours
## 11 2015-01-01 00:10:00   YES 0.00000000 hours
## 12 2015-01-01 00:11:00    NO 0.01666667 hours
## 13 2015-01-01 00:12:00    NO 0.03333333 hours
## 14 2015-01-01 00:13:00    NO 0.05000000 hours
## 15 2015-01-01 00:14:00   YES 0.00000000 hours
## 16 2015-01-01 00:15:00    NO 0.01666667 hours
## 17 2015-01-01 00:16:00    NO 0.03333333 hours
## 18 2015-01-01 00:17:00    NO 0.05000000 hours

#1


0  

DF<-read.table(text="datetime,alarm
1/1/2015 0:00,NO
1/1/2015 0:01,NO
1/1/2015 0:02,NO
1/1/2015 0:03,NO
1/1/2015 0:04,NO
1/1/2015 0:05,NO
1/1/2015 0:06,NO
1/1/2015 0:07,NO
1/1/2015 0:08,NO
1/1/2015 0:09,NO
1/1/2015 0:10,YES
1/1/2015 0:11,NO
1/1/2015 0:12,NO
1/1/2015 0:13,NO
1/1/2015 0:14,YES
1/1/2015 0:15,NO
1/1/2015 0:16,NO
1/1/2015 0:17,NO",header=TRUE,sep=",")

library(data.table)
setDT(DF)
#create grouping variable
DF[, period := rev(cumsum(rev(alarm == "YES")))]

DF[, datetime := as.POSIXct(datetime, "%m/%d/%Y %H:%M", tz = "UTC")]

#split-apply-combine operation
Res <- DF[, .(start = min(datetime),
       end = max(datetime)),
   by = period]

#possibly you need to add a minute here, don't know from your description    
Res[, length := difftime(end, start, unit = "hour")]

Res[, period := max(period) - period + 1]
#   period               start                 end           length
#1:      1 2015-01-01 00:00:00 2015-01-01 00:10:00 0.16666667 hours
#2:      2 2015-01-01 00:11:00 2015-01-01 00:14:00 0.05000000 hours
#3:      3 2015-01-01 00:15:00 2015-01-01 00:17:00 0.03333333 hours

#2


0  

df <- data.frame(datetime=c('1/1/2015 0:00','1/1/2015 0:01','1/1/2015 0:02','1/1/2015 0:03','1/1/2015 0:04','1/1/2015 0:05','1/1/2015 0:06','1/1/2015 0:07','1/1/2015 0:08','1/1/2015 0:09','1/1/2015 0:10','1/1/2015 0:11','1/1/2015 0:12','1/1/2015 0:13','1/1/2015 0:14','1/1/2015 0:15','1/1/2015 0:16','1/1/2015 0:17'),alarm=c('NO','NO','NO','NO','NO','NO','NO','NO','NO','NO','YES','NO','NO','NO','YES','NO','NO','NO'),stringsAsFactors=F);
df$datetime <- as.POSIXct(df$datetime,'%d/%m/%Y %H:%M',tz='UTC');
w <- which(df$alarm=='YES');
df$diff <- difftime(df$datetime,df$datetime[c(1L,w)[findInterval(seq_along(df$alarm),w)+1L]],units='hours');
df;
##               datetime alarm             diff
## 1  2015-01-01 00:00:00    NO 0.00000000 hours
## 2  2015-01-01 00:01:00    NO 0.01666667 hours
## 3  2015-01-01 00:02:00    NO 0.03333333 hours
## 4  2015-01-01 00:03:00    NO 0.05000000 hours
## 5  2015-01-01 00:04:00    NO 0.06666667 hours
## 6  2015-01-01 00:05:00    NO 0.08333333 hours
## 7  2015-01-01 00:06:00    NO 0.10000000 hours
## 8  2015-01-01 00:07:00    NO 0.11666667 hours
## 9  2015-01-01 00:08:00    NO 0.13333333 hours
## 10 2015-01-01 00:09:00    NO 0.15000000 hours
## 11 2015-01-01 00:10:00   YES 0.00000000 hours
## 12 2015-01-01 00:11:00    NO 0.01666667 hours
## 13 2015-01-01 00:12:00    NO 0.03333333 hours
## 14 2015-01-01 00:13:00    NO 0.05000000 hours
## 15 2015-01-01 00:14:00   YES 0.00000000 hours
## 16 2015-01-01 00:15:00    NO 0.01666667 hours
## 17 2015-01-01 00:16:00    NO 0.03333333 hours
## 18 2015-01-01 00:17:00    NO 0.05000000 hours