New day column starting from 1 in ascendent order ,every time 1 starts in column x1 , a new day number in the day column should be generated.

x1=c(1,1,1,1,1,2,2,1,1,2,2,2,2,2,2,1,1,1,1,1,2,2,2,2)

A=data.frame(x1)
library(dplyr)
A=A %>%
mutate(Day = (row_number()-1) %/% 7 + 1)
A
The result with this mutated above is different from what I would like.

## this is the result I would like to have 
   x1 Day
1   1   1
2   1   1
3   1   1
4   1   1
5   1   1
6   2   1
7   2   1
8   1   2
9   1   2
10  2   2
11  2   2
12  2   2
13  2   2
14  2   2
15  2   2
16  1   3
17  1   3
18  1   3
19  1   3
20  1   3
21  2   3
22  2   3
23  2   3
24  2   3

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.