Skip to content

ask for help

8 messages · Johnnycz, jim holtman, Peter Dalgaard +2 more

#
rle

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Wed, Aug 6, 2014 at 10:12 PM, Johnnycz <johnnycz at yeah.net> wrote:
#
[1]  1  6 16 23
[1]  4 12 18

Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Wed, Aug 6, 2014 at 7:12 PM, Johnnycz <johnnycz at yeah.net> wrote:
#
On 07 Aug 2014, at 11:16 , jim holtman <jholtman at gmail.com> wrote:

            
...with a little tinkering, like
[1]  1  4  6 12 16 18 23 34

then look at every 2nd element, discarding the last.
#
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:
#
For readability I like:
[1]  4 12 18
[1]  1  6 16 23
On 07 Aug 2014, at 17:23, William Dunlap <wdunlap at tibco.com> wrote:

            
#
Better:
b <- c(a[1]-1,a[-length(a)])
On 07 Aug 2014, at 17:28, Bart Kastermans <kasterma at kasterma.net> wrote:

            
#
I prefer the idiom
  c(TRUE, a[-1] != a[-length(x)])
because it works for character and other data types as well.

I also find that thinking in terms of runs instead of subscripting
tricks is easier.