Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sequentially label specific values in a series of values in R

I have a table that looks like this:

         Date Season
1  2022-01-01  Val_1
2  2022-01-02  Val_1
3  2022-01-03  Val_1
4  2022-01-04  Val_2
5  2022-01-05  Val_2
6  2022-01-06  Val_2
7  2022-01-07  Val_1
8  2022-01-08  Val_1
9  2022-01-09  Val_1
10 2022-01-10  Val_2
11 2022-01-11  Val_2
12 2022-01-12  Val_2
13 2022-01-13  Val_1
14 2022-01-14  Val_1
15 2022-01-15  Val_1

What I want to do is label each sequence of continuous Season values from 1 to the total number of continuous sequences that exist in the column, for every value that is in the column. I have seen similar solutions solved with function like rle, but I don’t currently see how to mold it to this problem. Here is an example of the output I want:

         Date Season Season_Num
1  2022-01-01  Val_1          1
2  2022-01-02  Val_1          1
3  2022-01-03  Val_1          1
4  2022-01-04  Val_2          1
5  2022-01-05  Val_2          1
6  2022-01-06  Val_2          1
7  2022-01-07  Val_1          2
8  2022-01-08  Val_1          2
9  2022-01-09  Val_1          2
10 2022-01-10  Val_2          2
11 2022-01-11  Val_2          2
12 2022-01-12  Val_2          2
13 2022-01-13  Val_1          3
14 2022-01-14  Val_1          3
15 2022-01-15  Val_1          3

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

With a single mutate call, using cumsum and lag:

library(dplyr)
df %>% 
  mutate(Season_num = cumsum(Season == "Val_1" & lag(Season, default = "Val_2") != Season))

#          Date Season Season_num
# 1  2022-01-01  Val_1          1
# 2  2022-01-02  Val_1          1
# 3  2022-01-03  Val_1          1
# 4  2022-01-04  Val_2          1
# 5  2022-01-05  Val_2          1
# 6  2022-01-06  Val_2          1
# 7  2022-01-07  Val_1          2
# 8  2022-01-08  Val_1          2
# 9  2022-01-09  Val_1          2
# 10 2022-01-10  Val_2          2
# 11 2022-01-11  Val_2          2
# 12 2022-01-12  Val_2          2
# 13 2022-01-13  Val_1          3
# 14 2022-01-14  Val_1          3
# 15 2022-01-15  Val_1          3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading