如何在另一列的列中检测和标记更改

时间:2015-05-31 18:08:08

标签: r data-manipulation

我正在尝试标记流程的开始和结束时间。 代码需要检测更改何时开始以及何时结束,在另一列中标记它。

示例数据:

date  process 
2007     0            
2008     1
2009     1
2010     1
2011     1
2012     1
2013     0

目标:

date  process        Status
2007     0             NA
2008     1        Process_START
2009     1             NA
2010     1             NA
2011     1             NA
2012     1        Process_END
2013     0             NA

2 个答案:

答案 0 :(得分:4)

可能通过计算diff并将其滞后于两个方向:

dif <- diff(df1$process)
df1$Status <- factor(c(NA, dif) - 2 * c(dif, NA), levels = -3:3)
levels(df1$Status) <- c(rep(NA, 4), "Start", "End", "Start&End")
#   date process Status
# 1 2007       0   <NA>
# 2 2008       1  Start
# 3 2009       1   <NA>
# 4 2010       1   <NA>
# 5 2011       1   <NA>
# 6 2012       1    End
# 7 2013       0   <NA>

更新

没有因素的版本:

dif <- diff(df1$process)
df1$Status <- c(NA, dif) - 2 * c(dif, NA)
df1$Status <- c(rep(NA,4), "Start", "End", "Start&End")[df1$Status + 4]

请注意,如果是一年的流程,您可以使用&#34;开始&amp;端&#34;情况。

更新2

如果系列以process = 1开始(或结束),则预期输出可能不是NA而是Start(或End):

dif <- diff(df1$process)
df1$Status <- c(df1$process[1], dif) - 2 * c(dif, -tail(df1$process,1))
df1$Status <- c(rep(NA,4), "Start", "End", "Start&End")[df1$Status + 4]

更复杂的例子:

set.seed(4)
df1 <- data.frame(date = 2007:(2007+24), process = sample(c(0,1, 1), 25, TRUE))

最后一个版本产生:

#    date process    Status
# 1  2007       1 Start&End
# 2  2008       0      <NA>
# 3  2009       0      <NA>
# 4  2010       0      <NA>
# 5  2011       1 Start&End
# 6  2012       0      <NA>
# 7  2013       1     Start
# 8  2014       1      <NA>
# 9  2015       1       End
# 10 2016       0      <NA>
# 11 2017       1 Start&End
# 12 2018       0      <NA>
# 13 2019       0      <NA>
# 14 2020       1     Start
# 15 2021       1      <NA>
# 16 2022       1      <NA>
# 17 2023       1      <NA>
# 18 2024       1      <NA>
# 19 2025       1      <NA>
# 20 2026       1      <NA>
# 21 2027       1      <NA>
# 22 2028       1      <NA>
# 23 2029       1      <NA>
# 24 2030       1      <NA>
# 25 2031       1       End

答案 1 :(得分:3)

data.table

的一个选项
library(data.table)#v1.9.5+
setDT(df1)[, gr:= rleid(process)][,Status:=NA_character_][process==1, 
   Status:=replace(Status, 1:.N %in% c(1, .N), c('Process_START', 
      'Process_END')) , gr][,gr:= NULL]
#     date process        Status
# 1: 2007       0            NA
# 2: 2008       1 Process_START
# 3: 2009       1            NA
# 4: 2010       1            NA
# 5: 2011       1            NA
# 6: 2012       1   Process_END
# 7: 2013       0            NA

更新

或者修改

 setDT(df1)[, gr:= rleid(process)][process==1L, Status:=c(NA, 
'Process_START', 'Process_END', 'Process_START_END')[(1:.N==1L) +
        2*(1:.N==.N)+1] , gr][,gr:=NULL]
  #   date process        Status
  #1: 2007       0            NA
  #2: 2008       1 Process_START
  #3: 2009       1            NA
  #4: 2010       1            NA
  #5: 2011       1            NA
  #6: 2012       1   Process_END
  #7: 2013       0            NA

使用@David Arenburg评论中的例子

 setDT(df1)[, gr:= rleid(process)][process==1L, Status:=c(NA, 
    'Process_START', 'Process_END', 'Process_START_END')[(1:.N==1L) + 
       2*(1:.N==.N)+1] , gr][,gr:=NULL]
  #   date process        Status
  #1: 2007       0            NA
  #2: 2008       1 Process_START
  #3: 2009       1            NA
  #4: 2010       1            NA
  #5: 2011       1            NA
  #6: 2012       1   Process_END
  #7: 2013       0            NA
  #8: 2013       0            NA
  #9: 2013       1 Process_START
  #10:2013       1   Process_END
  #11:2013       0            NA
  #12:2013       1 Process_START
  #13:2013       1   Process_END

对于@ bergant的帖子中的复杂例子

  setDT(df1)[, gr:= rleid(process)][process==1L, Status:=c(NA, 
    'Process_START', 'Process_END', 'Process_START_END')[(1:.N==1L) + 
       2*(1:.N==.N)+1] , gr][,gr:=NULL]
 #    date process gr            Status
 # 1: 2007       1  1 Process_START_END
 # 2: 2008       0  2                NA
 # 3: 2009       0  2                NA
 # 4: 2010       0  2                NA
 # 5: 2011       1  3 Process_START_END
 # 6: 2012       0  4                NA
 # 7: 2013       1  5     Process_START
 # 8: 2014       1  5                NA
 # 9: 2015       1  5       Process_END
 #10: 2016       0  6                NA
 #11: 2017       1  7 Process_START_END
 #12: 2018       0  8                NA
 #13: 2019       0  8                NA
 #14: 2020       1  9     Process_START
 #15: 2021       1  9                NA
 #16: 2022       1  9                NA
 #17: 2023       1  9                NA
 #18: 2024       1  9                NA
 #19: 2025       1  9                NA
 #20: 2026       1  9                NA
 #21: 2027       1  9                NA
 #22: 2028       1  9                NA
 #23: 2029       1  9                NA
 #24: 2030       1  9                NA
 #25: 2031       1  9       Process_END