& statement within an ifelse Loop

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

& statement within an ifelse Loop

Phillip Heinrich
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: & statement within an ifelse Loop

David Winsemius

On 9/21/19 12:57 PM, Phillip Heinrich wrote:

> 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))


The first thing to do is clarify which package you are expecting lag to
come from. I suspect it from a package other than the "base" stats which
gives this.


lag(c(1,2)) == c(1,2)
[1] TRUE TRUE
attr(,"tsp")
[1] 0 1 1


The base lag function only changes the tsp attributes, but not the values.


--

David.

>> 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-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: & statement within an ifelse Loop

Jeff Newmiller
In reply to this post by Phillip Heinrich
You really need to address the responses to your previous posts on this topic. [1][2] What happened when you tested those methods? In particular, what about them was so ineffective that you are still here asking the same question? And why are you starting new threads of conversation on the same topic?

Failing to respond to help is rude, but ignoring help is really rude. You may
just be unfamiliar with mailing lists, but now is the time to rectify your behavior. Read the Posting Guide and consider sending reply-all emails at least to the messages I reference below.

[1] https://stat.ethz.ch/pipermail/r-help/2019-September/464183.html
[2] https://stat.ethz.ch/pipermail/r-help/2019-September/464202.html


On September 21, 2019 12:57:50 PM PDT, Phillip Heinrich <[hidden email]> wrote:

>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-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.