Home > Software design >  Mark Beginning and End of Runs in R
Mark Beginning and End of Runs in R

Time:10-16

I have the following table and want to make a "Beginning" and "End" column with T/F values:

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

How can this be done in R?

Many thanks

CodePudding user response:

Base:

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

tidyverse:

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

CodePudding user response:

Base R Solution:

# 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("Begining", "End")]  <- t(
  outer(
    c(1, -1), 
    with(
      df,
      diff(
        c(
          0,
          Trigger
        )
      )
    ),
    `==`
  )
)

tidyverse():: solution:

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

data.table():: solution:

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