I have data collected continuously over consecutive days. Each observation already contains the variables datetime (a POSIXct object) and par (a numeric object). I want to create a new variable called ID which will be a unique number associated with an individual night-time period. I will define night-time as all observations where par = 0. An example of what I would like my data to look like is given below for 3 consecutive days.
Note: my actual data is taken at 15-minute intervals, but I truncated this example for easier viewing.
              datetime    par    ID
1  2015-04-23 00:00:00    0.0     1
2  2015-04-23 08:00:00    0.0     1
3  2015-04-23 12:00:00  817.7     0
4  2015-04-23 19:00:00    0.0     2
5  2015-04-24 00:00:00    0.0     2
6  2015-04-24 08:00:00    0.0     2
7  2015-04-24 12:00:00  269.9     0
8  2015-04-24 19:00:00    0.0     3
9  2015-04-25 00:00:00    0.0     3
10 2015-04-25 08:00:00    0.0     3
11 2015-04-25 12:00:00 1701.8     0
12 2015-04-25 19:00:00    0.0     4
13 2015-04-25 23:00:00    0.0     4
I would like non-night (par !=0) observations have ID = 0. The first night-time period occurs on 4/23/2015, so I want it's ID = 1. I then want to add 1 to each following night-time period ID.
Any ideas? Despite much effort, I have not been able to achieve the above result. Thank you in advance.
                        
With
cumsum: