I would like to create a 'segment' ID so that:
- If the value (in one column) is the same as the row before you maintain the same segment ID
- However, if the value (in one column) is different than the row before the segment ID increments by one
I am currently trying to achieve this via:
require(dplyr)
person <- c("Mark","Mark","Mark","Mark","Mark","Steve","Steve","Tim", "Tim", "Tim","Mark")
df <- data.frame(person,stringsAsFactors = FALSE)
df$segment = 1
df$segment <- ifelse(df$person == dplyr::lag(df$person),dplyr::lag(df$segment),dplyr::lag(df$segment)+1)
But I am not getting the desired result through this method.
Any help would be appreciated