Skip to content
Prev 342937 / 398506 Next

ask for help

My solution may be a bit clearer if you define the function isFirstInRun
isFirstInRun <- function(x) {
   if (length(x) == 0) {
      logical(0)
   } else {
      c(TRUE, x[-1] != x[-length(x)])
   }
}

Then that solution is equivalent to
   which(isFirstInRun(a) & a==1)

If 'a' contains NA's then you have to decide how to deal with them.

(The call to 'which' is not needed if you are going to be using the
result as a subscript.)

You may also want isLastInRun
isLastInRun <- function(x) {
   if (length(x) == 0) {
      logical(0)
   } else {
      c(x[-1] != x[-length(x)], TRUE)
   }
}
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Thu, Aug 7, 2014 at 7:36 AM, William Dunlap <wdunlap at tibco.com> wrote: