conditional increase by increment

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

conditional increase by increment

penguins
I am trying to assign increasing trip numbers to a binary variable ("land"; 1=home and 0=away) where a string of 1's shouldn't increment the trip_no more than once.

For example; based on
land<-c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0)
the "trip_no" sequence produced should be   1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3

This is as far as I can get but Im stumped. In addition I need it to work on data where the land variable can start on "0" or "1" for trip_no=1. Any help would be hugely appreciated:

land<-c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0)
trip_no <- rep(0, length(land))
gg<-cbind(land,trip_no)

increment <- function(x){
  eval.parent(substitute(x <- x + 1))
}

    for(i in length(gg)){
      if(gg$land[[i]]==1) {
          gg$trip_no<-increment(trip_no[i])
        }
     }
Reply | Threaded
Open this post in threaded view
|

Re: conditional increase by increment

William Dunlap
> land<-c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0)
> expect <- c(1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3)
> cumsum(c(TRUE, diff(land)==1))
 [1] 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3
> all.equal(.Last.value, expect)
[1] TRUE

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of penguins
> Sent: Wednesday, July 18, 2012 10:13 AM
> To: [hidden email]
> Subject: [R] conditional increase by increment
>
> I am trying to assign increasing trip numbers to a binary variable ("land";
> 1=home and 0=away) where a string of 1's shouldn't increment the trip_no
> more than once.
>
> For example; based on
> land<-c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0)
> the "trip_no" sequence produced should be   1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3
>
> This is as far as I can get but Im stumped. In addition I need it to work on
> data where the land variable can start on "0" or "1" for trip_no=1. Any help
> would be hugely appreciated:
>
> land<-c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0)
> trip_no <- rep(0, length(land))
> gg<-cbind(land,trip_no)
>
> increment <- function(x){
>   eval.parent(substitute(x <- x + 1))
> }
>
>     for(i in length(gg)){
>       if(gg$land[[i]]==1) {
>           gg$trip_no<-increment(trip_no[i])
>         }
>      }
>
> --
> View this message in context: http://r.789695.n4.nabble.com/conditional-increase-
> by-increment-tp4636910.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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
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: conditional increase by increment

penguins
In reply to this post by penguins
Thanks William, that works fantastically!

I had a quick play with my data and have realised a potential problem in that if an individual ends the series at home it records an additional trip-no when one wasnt made. I was wondering whether you could think of a way to alter it slightly so that the increment works on the 0 rather than the 1;

for example:
land<-c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0)
the "trip_no" sequence produced would be   1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,3

Many thanks
Reply | Threaded
Open this post in threaded view
|

Re: conditional increase by increment

Peter Ehlers
On 2012-07-19 05:56, penguins wrote:

> Thanks William, that works fantastically!
>
> I had a quick play with my data and have realised a potential problem in
> that if an individual ends the series at home it records an additional
> trip-no when one wasnt made. I was wondering whether you could think of a
> way to alter it slightly so that the increment works on the 0 rather than
> the 1;
>
> for example:
> land<-c(0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,0)
> the "trip_no" sequence produced would be   1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,3

[It would have been nice if you had included context - not everyone
uses the Nabble interface.]

Bill Dunlap's solution to your problem as originally stated was:

   cumsum(c(TRUE, diff(land)==1))

I think that your new requirement is satisfied with

   cumsum(c(TRUE, diff(land) == -1))

Unless, of course, I'm misinterpreting something.

Peter Ehlers

>
> Many thanks
>
> --
> View this message in context: http://r.789695.n4.nabble.com/conditional-increase-by-increment-tp4636910p4637029.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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
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.