如何以最少的时间使状态保持在重复状态中

时间:2018-07-02 22:22:49

标签: r dataframe dplyr

说我有来自多个内部版本的状态数据,这些状态数据会在收到警报时报告不同的警报:

build <- c(rep(11243, 6), rep(12640, 4))

message <- c(rep("High Temp", 2), rep("No Alarm", 2), rep("High Temp", 2), 
"Low Temp", rep("No Alarm", 2), "High Temp")

timeEntered <- c("2018-06-04 08:00:00", "2018-06-04 09:00:00", "2018-06-05 
08:00:00", "2018-06-06 08:00:00", "2018-06-06 09:00:00", "2018-06-07 08:08:00", 
"2017-05-23 12:45:00", "2017-05-23 13:00:00", "2017-05-24 12:30:00", "2017-05-24 12:45:00")

data <- data.frame(build, message, 
         timeEntered = as.POSIXct(timeEntered, format = "%Y-%m-%d %H:%M:%S"))

输出:

build   message         timeEntered
11243 High Temp 2018-06-04 08:00:00
11243 High Temp 2018-06-04 09:00:00  
11243  No Alarm 2018-06-05 08:00:00
11243  No Alarm 2018-06-06 08:00:00
11243 High Temp 2018-06-06 09:00:00
11243 High Temp 2018-06-07 08:08:00
12640  Low Temp 2017-05-23 12:45:00
12640  No Alarm 2017-05-23 13:00:00
12640  No Alarm 2017-05-24 12:30:00
12640 High Temp 2017-05-24 12:45:00

我希望通过构建来获取每种消息类型的最短时间,同时保留我收到消息的顺序。例如,我的最终目标是拥有一个像这样的数据框:

输出:

build   message         timeEntered
11243 High Temp 2018-06-04 08:00:00
11243  No Alarm 2018-06-05 08:00:00
11243 High Temp 2018-06-06 09:00:00
12640  Low Temp 2017-05-23 12:45:00
12640  No Alarm 2017-05-23 13:00:00
12640 High Temp 2017-05-24 12:45:00

提前谢谢您...

1 个答案:

答案 0 :(得分:0)

data%>%
   group_by(s=cumsum(lag(message,default = message[1])!=message),build,message)%>%
   summarise(timeEntered=min(timeEntered))
# A tibble: 6 x 4
# Groups:   s, build [?]
      s build message   timeEntered        
  <int> <dbl> <chr>     <dttm>             
1     0 11243 High Temp 2018-06-04 08:00:00
2     1 11243 No Alarm  2018-06-05 08:00:00
3     2 11243 High Temp 2018-06-06 09:00:00
4     3 12640 Low Temp  2017-05-23 12:45:00
5     4 12640 No Alarm  2017-05-23 13:00:00
6     5 12640 High Temp 2017-05-24 12:45:00

使用基本R:

a=rle(data$message)
a$values=1:length(a$lengths)
aggregate(timeEntered~.,cbind(data,b=inverse.rle(a)),min)

  build   message b         timeEntered
1 11243 High Temp 1 2018-06-04 08:00:00
2 11243  No Alarm 2 2018-06-05 08:00:00
3 11243 High Temp 3 2018-06-06 09:00:00
4 12640  Low Temp 4 2017-05-23 12:45:00
5 12640  No Alarm 5 2017-05-23 13:00:00
6 12640 High Temp 6 2017-05-24 12:45:00