I work with data frames for flight movements (~ 1 million rows * 108 variables) and want to group phases during which a certain criterion is fulfilled (i.e. the value of a certain variable). To identify these groups, I want to number them. As a newbie to R, I did this for my business. Now I am looking for a more elegant way. In particular, I would like to bridge the “useless” gaps in group numbering. I provide a simplified example of my dplyr data frame with a THR value for a threshold criterion. Lines are sorted by timestamp (and therefore, I can truncate this here).
THR <- c(13,17,19,22,21,19,17,12,12,17,20,20,20,17,17,13, 20,20,17,13)
df <- as.data.frame(THR)
df <- tbl_df(df)
To mark all lines where (non) criteria are met
df <- mutate(df, CRIT = THR < 19)
At the following, I was able to conditionally "cumsum" get a unique group identification:
df <- mutate(df, GRP = ifelse(CRIT == 1, 0, cumsum(CRIT))
df
x CRIT GRP
1 13 TRUE 0
2 17 TRUE 0
3 19 FALSE 2
4 22 FALSE 2
5 21 FALSE 2
6 19 FALSE 2
7 17 TRUE 0
8 12 TRUE 0
9 12 TRUE 0
10 17 TRUE 0
11 20 FALSE 6
12 20 FALSE 6
Although this is a trick, and I can work with groups with group_by (for example, summarize, filter), the numbering is not perfect, as you can see in the output example. In this example, the 1st number is numbered 2, and the second group is numbered 6, which corresponds to the result of cumsum ().
I would appreciate it if someone could shed light on me. I could not find a suitable solution in other posts.