On the below data:
wpd c_days c_days_ct cd_days_ct_r
-72.88 0 0 0
-56 0 0 0
-0.49 1 1 1
-32.41 0 0 0
-83.69 0 0 0
-53.15 0 0 0
-0.42 1 1 1
-52.32 0 0 0
-26.37 0 0 0
-45.57 0 0 0
-23.62 0 0 0
-1.22 1 1 1
-23.94 0 0 0
-0.13 1 1 1
-13.81 1 2 1
-9.58 1 3 1
0 1 4 1
-1.95 1 5 1
-8.85 1 6 1
-15.66 1 7 1
-15.87 1 8 1
-53.94 0 0 0
-91.84 0 0 0
-58.89 0 0 0
-61.52 0 0 0
What I am trying to do
calculating c_days based on the value of WPD (>=-20) and setting this equal to 1 (line 1 of code below)
The above data shows cd_days_ct (from excel) and is checking whether the current row value of c_days and if so, from the previous value of c_days_ct, add 1. I am trying to calculate a rolling counter of the number of times c_days = 1 consecutively
I am trying to do this via line #3 of the code below (using lag) I had to add line 2 of the code to create c_days_ct since if missing, got the error (below) Error: Problem with
mutate()inputcdays_ct. x object 'cdays_ct' not foundBut c_days_ct_r never increments above 1 and I can't figure out why
mutate(cdays=ifelse(wpd>=-20,1,0)) %>% mutate(cdays_ct=0) %>% mutate(cdays_ct=if_else(cdays==1,lag(cdays_ct,1)+1,0))
Probably something basic that I am missing but I have been hitting my head against the wall now for two days