我是R的新手,所以需要对我正在尝试编码的问题提供一些帮助。 下面是一个数据集,我需要计算自警报以来的天数。我有时间序列数据,其中每秒捕获值,并且在第10秒发生警报。我想计算从第10秒到第1秒的时间之间的差异,并在几小时内将其填入单独的列中。一旦第一个警报的值为0,即差值为零,下一行应考虑下一个警报并填充警报后的天数。下面是样本数据
PIT >> WARNING : Slave exited abnormally due to TIMED_OUT
感谢。
答案 0 :(得分: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
答案 1 :(得分:0)
config.logger = Logger.new(STDOUT)
config.logger.level = Logger.const_get('INFO')
config.log_level = :info