一种为“ bupaR”准备事件日志的循环方法:可以将其向量化吗?

时间:2019-01-25 17:22:32

标签: r event-log

背景

我有来自PLC的真实数据,准备转换为事件日志以与软件包bupaR一起使用。 以下数据受到限制和简化,但包含有关资源,时间戳,状态类型和event_ID的信息。

我用循环实现了下面记录的所需转换。我的问题是,可以通过“向量化”方式在没有循环的情况下完成此操作吗?

目标:

我要

  • 检测何时发生错误,我想对其进行跟踪直到结束。当“ State_type”不是“ Error”,“ Comlink Down”,“ Not Active”时,它结束。
  • 为同一“错误跟踪”的所有行分配一个错误号(分配给“ Error_ID”)
  • 具有错误的开始时间(第一个错误行的时间戳)(分配给“ Error_startTS”)
  • 具有错误的结束时间(错误后第一行的时间戳,换句话说, 结束错误的事件的时间戳)((分配给“ Error_endTS”))
  • 为错误的行“开始”或“进行中”分配 Life_cycle_ID 。 (在稍后的阶段中,“完成” Life_cycle_id 将插入到每个“错误跟踪”的“进行中”的最后一行之后;当时的一个问题;-)

我的数据

my_df <- 
  structure(list(Resource = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), 
                                      .Label = c("L54", "L60", "L66", "L68", "L70", "L76", 
                                                 "L78", "L95", "L96", "L97", "L98", "L99"), 
                                      class = "factor"), 
                 Datetime_local = structure(c(1535952594, 1535952618, 1535952643, 1535952651, 
                                              1535952787, 1535952835, 1535952840, 1535952846, 
                                              1535952890, 1535952949, 1535952952, 1535952958, 
                                              1535953066), 
                                            class = c("POSIXct", "POSIXt"), tzone = ""), 
                 State_type = structure(c(6L, 4L, 8L, 4L, 8L, 4L, 12L, 4L, 8L, 4L, 12L, 4L, 12L), 
                                        .Label = c("Comlink Down", "Comlink Up", "Counter", 
                                                   "Error", "Message", "No part in", "No part out", 
                                                   "Not active", "Part changing", "Part in", "Part out", 
                                                   "Producing", "Waiting"), 
                                        class = "factor"), 
                 event_ID = c("e00000000000072160", "e00000000000072270", "e00000000000072400", 
                              "e00000000000072430", "e00000000000072810", "e00000000000073110", 
                              "e00000000000073150", "e00000000000073170", "e00000000000073300", 
                              "e00000000000073520", "e00000000000073540", "e00000000000073570", 
                              "e00000000000074040")), 
            .Names = c("Resource", "Datetime_local", "State_type", "event_ID"), 
            row.names = 160:172, class = "data.frame")

...看起来像这样

    Resource      Datetime_local State_type           event_ID
160      L60 2018-09-03 07:29:54 No part in e00000000000072160
161      L60 2018-09-03 07:30:18      Error e00000000000072270
162      L60 2018-09-03 07:30:43 Not active e00000000000072400
163      L60 2018-09-03 07:30:51      Error e00000000000072430
164      L60 2018-09-03 07:33:07 Not active e00000000000072810
165      L60 2018-09-03 07:33:55      Error e00000000000073110
166      L60 2018-09-03 07:34:00  Producing e00000000000073150
167      L60 2018-09-03 07:34:06      Error e00000000000073170
168      L60 2018-09-03 07:34:50 Not active e00000000000073300
169      L60 2018-09-03 07:35:49      Error e00000000000073520
170      L60 2018-09-03 07:35:52  Producing e00000000000073540
171      L60 2018-09-03 07:35:58      Error e00000000000073570
172      L60 2018-09-03 07:37:46  Producing e00000000000074040

我的UDF:

AssignErrorNumber <- function(df) {

  # set start values
  require(dplyr)
  errorNumber <- 0
  i <- 1
  j <- 0
  df$Error_ID <- 0
  df$Error_startTS <- NA
  df$Error_endTS <- NA
  df$Lifecycle_ID <- NA

  # loop through all rows
  while (i <= nrow(df)) {

    ## find the first row with an error raised
    if ( df$State_type[i] == "Error") {

      # for the first row for this error, 
      #   increase error counter and get startTS
      #   save them for this row
      errorNumber <- errorNumber + 1
      startTS <- df$Datetime_local[i] 
      df$Error_ID[i] <- errorNumber
      df$Error_startTS[i] <- startTS
      df$Lifecycle_ID[i] <- "Start"

      # do the following for each following row 
      #   until state_type goes to non-error state
      #   save error_number and startTS for this row
      i <- i+1
      j <- 1 # counter for the loop
      while (df$State_type[i] %in% c("Error", "Comlink Down", "Not active")) {
        df$Error_ID[i] <- errorNumber
        df$Error_startTS[i] <- startTS
        df$Lifecycle_ID[i] <- "Ongoing"
        i <- i+1 
        j <- j+1
      }
      # we saw the last row for this error, mark as "ongoing" AND add a row later on with "complete"
      # alternatively we could mark this as "completed", but this
      # mixes things up: the time when an error is finished is not this Datetime_local!
      if (j!=1){ # if not first line, this should remain "start"
        df$Lifecycle_ID[i-1] <- "Ongoing"
      }

    }
    # before going to the next row, 
    #   get TS from the row following the last error-row (if not end of file)
    #   go back and set endTS for this error_number
    if (i <= nrow(df)) {
      endTS <- df$Datetime_local[i]
    }
    else {
      endTS <- df$Datetime_local[i-1] # last row: endTS = startTS of last row of error
    }

    while (j >= 1) {
      df$Error_endTS[i-j] <- endTS
      j <- j-1
    }

    # to go to next row
    i <- i+1
  }
  # transform TS's to Date and time POSIXct
  df$Error_startTS <- as.POSIXct(df$Error_startTS, origin = "1970-01-01")
  df$Error_endTS <- as.POSIXct(df$Error_endTS, origin = "1970-01-01")
  return(df)
}

呼叫UDF

AssignErrorNumber(my_df)

想要的输出//我的循环函数的输出

Resource      Datetime_local State_type           event_ID Error_ID       Error_startTS         Error_endTS Lifecycle_ID
160      L60 2018-09-03 07:29:54 No part in e00000000000072160        0                <NA>                <NA>         <NA>
161      L60 2018-09-03 07:30:18      Error e00000000000072270        1 2018-09-03 07:30:18 2018-09-03 07:34:00        Start
162      L60 2018-09-03 07:30:43 Not active e00000000000072400        1 2018-09-03 07:30:18 2018-09-03 07:34:00      Ongoing
163      L60 2018-09-03 07:30:51      Error e00000000000072430        1 2018-09-03 07:30:18 2018-09-03 07:34:00      Ongoing
164      L60 2018-09-03 07:33:07 Not active e00000000000072810        1 2018-09-03 07:30:18 2018-09-03 07:34:00      Ongoing
165      L60 2018-09-03 07:33:55      Error e00000000000073110        1 2018-09-03 07:30:18 2018-09-03 07:34:00      Ongoing
166      L60 2018-09-03 07:34:00  Producing e00000000000073150        0                <NA>                <NA>         <NA>
167      L60 2018-09-03 07:34:06      Error e00000000000073170        2 2018-09-03 07:34:06 2018-09-03 07:35:52        Start
168      L60 2018-09-03 07:34:50 Not active e00000000000073300        2 2018-09-03 07:34:06 2018-09-03 07:35:52      Ongoing
169      L60 2018-09-03 07:35:49      Error e00000000000073520        2 2018-09-03 07:34:06 2018-09-03 07:35:52      Ongoing
170      L60 2018-09-03 07:35:52  Producing e00000000000073540        0                <NA>                <NA>         <NA>
171      L60 2018-09-03 07:35:58      Error e00000000000073570        3 2018-09-03 07:35:58 2018-09-03 07:37:46        Start
172      L60 2018-09-03 07:37:46  Producing e00000000000074040        0                <NA>                <NA>         <NA>

衷心感谢阅读了这个冗长问题的任何人。 我重复我的问题:“ 这组问题可以向量化吗?

0 个答案:

没有答案