Home > Net >  Sequentially label specific values in a series of values in R
Sequentially label specific values in a series of values in R

Time:10-04

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

CodePudding user response:

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

CodePudding user response:

We could get the diff between the date objects after grouping by 'Season' and do the cumulative sum

library(dplyr)
df1 %>%
   group_by(Season) %>%
   mutate(Season_Num = cumsum(c(TRUE, diff(Date) != 1))) %>%
   ungroup

-output

# A tibble: 15 × 3
   Date       Season Season_Num
   <date>     <chr>       <int>
 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

data

df1 <- structure(list(Date = structure(c(18993, 18994, 18995, 18996, 
18997, 18998, 18999, 19000, 19001, 19002, 19003, 19004, 19005, 
19006, 19007), class = "Date"), Season = c("Val_1", "Val_1", 
"Val_1", "Val_2", "Val_2", "Val_2", "Val_1", "Val_1", "Val_1", 
"Val_2", "Val_2", "Val_2", "Val_1", "Val_1", "Val_1")), row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15"), class = "data.frame")
  • Related