# & statement within an ifelse Loop

3 messages
Open this post in threaded view
|

## & statement within an ifelse Loop

 Still putzing around trying to increment a count vector when the date changes.            Date count 1   2018-03-29     1 2   2018-03-29     1 3   2018-03-29     1 81  2018-03-30     1 82  2018-03-30     1 83  2018-03-30     1 165 2018-03-31     1 166 2018-03-31     1 167 2018-03-31     1   I can get count to change when the date changes  - lines 81 and 165 - by comparing the date to the date on the previous line (lag(Date,1)) but then the count returns to 1 on line 82 and line 166. test2 <- transform(test2, +                   count = ifelse(Date == lag(Date,1),count,count+1)) > test2           Date count 1   2018-03-29    NA 2   2018-03-29     1 3   2018-03-29     1 81  2018-03-30     2 82  2018-03-30     1 83  2018-03-30     1 165 2018-03-31     2 166 2018-03-31     1 167 2018-03-31     1 test2 <- transform(test2, +           count = ifelse(Date == lag(Date,1),(lag(count,1)),(lag(count,1)+1))) With the code above I get the same results.  It seems to me that line 82 should have count = 2 since the dates on line 81 and 82 are the same so the count from line 82 should be the same as 81 -  (lag(count,1)).  Similarly, if line 83 were count = 2 then line 165 should be equal to 3. What am I missing here?  Is there a way to add an & clause to either the if or the else clause such as: ((-2:2) >= 0) & ((-2:2) <= 0)I’ve tried this several different ways such as: (lag(count,1)) &(count = count+1).   with no success. Thanks, Philip         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.