I have a dataframe where one column indicates text line
s, which are only partially consecutive:
df <- data.frame(
line = c("0001","0002", "0003", "0011","0012","0234","0235","0236")
)
I want to group the rows based on consecutive line numbers to get this expected result:
df
line grp
1 0001 1
2 0002 1
3 0003 1
4 0011 2
5 0012 2
6 0234 3
7 0235 3
8 0236 3
I've tried to approach this with dplyr
's lag
function but am stuck there:
library(dplyr)
df %>%
mutate(line = as.numeric(line),
diff = abs(lag(line) - line))