在 R 中标记运行的开始和结束

Mark Beginning and End of Runs in R

我有以下 table 并想用 T/F 值创建一个“开始”和“结束”列:

Trigger Beginning End
1 T F
1 F F
0 F T
1 T F
0 F T
1 T F
1 F F

如何在 R 中完成此操作?

非常感谢

基地:

df <- data.frame(Trigger = c(1, 1, 0, 1, 0, 1, 1))
df$Beginning <- df$Trigger & !c(0, df$Trigger[-nrow(df)]) 
df$End <- !df$Trigger & c(0, df$Trigger[-nrow(df)]

整洁宇宙:

mutate(
  df, 
  Beginning = Trigger & !lag(Trigger, default = FALSE),
  End = !Trigger & lag(Trigger, default = TRUE)  # May want to use FALSE depending on use case.
)  

基础 R 解决方案:

# Import data: df => data.frame
df <- structure(list(Trigger = c(1L, 1L, 0L, 1L, 0L, 1L, 1L)),
                row.names = c(NA, 7L), class = "data.frame")

# Determine if an event has begun or ended: df => data.frame
df[,c("Beginning", "End")]  <- t(
  outer(
    c(1, -1), 
    with(
      df,
      diff(
        c(
          0,
          Trigger
        )
      )
    ),
    `==`
  )
)

tidyverse()::解法:

library(tidyverse)
df %>%
  transmute(event_diff = c(if_else(first(Trigger) == 1, 1, 0), diff(Trigger))) %>% 
  map2(., c(1, -1), `==`) %>% 
  set_names(., c("Beginning", "End")) %>% 
  bind_cols(Trigger = df$Trigger, .)

data.table()::解法:

library(data.table)
dt <- data.table(df)
dt[,trigger_diff := diff(c(0, Trigger)),]
dt[,c("Beginning", "End") := lapply(c(1, -1), function(x){trigger_diff == x}),]
dt[,trigger_diff := NULL, ]