Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在重复状态行中以最短时间保持状态_R_Dataframe_Dplyr - Fatal编程技术网

R 如何在重复状态行中以最短时间保持状态

R 如何在重复状态行中以最短时间保持状态,r,dataframe,dplyr,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"

假设我有来自多个构建的状态数据,这些构建在接收到不同的警报时报告不同的警报:

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  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

提前谢谢你

那么,@Onyambu,
cumsum
正在跟踪自从第一条“消息”输入以来,在滞后比较中发生了多少不匹配的情况?@Kyle当然可以。但是您可以使用
rle
,因为这是
rle
的工作,这是正确的。我没有想到,
rle
能创造奇迹。非常感谢。
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
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
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