Skip to content
Prev 246675 / 398502 Next

algorithm help

You need to be more careful about the first
and last rows in the dataset.  I think yours
only works when a starts with 0 and ends with 1.

  > f.fragment(c(1,1,0,0), c(11,12,13,14))
       stretch start end no.of.1s
  [1,]       1    NA  12        2
  > f.fragment(c(1,1,0,1), c(11,12,13,14))
       stretch start end no.of.1s
  [1,]       1    14  12        2
  [2,]       1    14  14        1
  > f.fragment(c(0,1,0,1), c(11,12,13,14))
       stretch start end no.of.1s
  [1,]       1    12  12        1
  [2,]       2    14  14        1
  > f.fragment(c(0,1,0,0), c(11,12,13,14))
       stretch start end no.of.1s
  [1,]       1    12  12        1
  [2,]       2    NA  12        1
  > f.fragment(c(1,1,1,1), c(11,12,13,14))
       stretch end no.of.1s
  [1,]       1  14        4
  [2,]       0  14        4
  > f.fragment(c(0,0,0,0), c(11,12,13,14))
       stretch start
  [1,]       1    NA

The following does better.  It keeps things as
logical vectors as long as possible, which tends
to work better when dealing with runs.
  f <- function(a, b) {
       isFirstIn1Run <- c(TRUE, a[-1] != a[-length(a)]) & a==1
       isLastIn1Run <- c(a[-1] != a[-length(a)], TRUE) & a==1
       data.frame(stretch=seq_len(sum(isFirstIn1Run)),
                  start = b[isFirstIn1Run],
                  end = b[isLastIn1Run],
                  no.of.1s = which(isLastIn1Run) - which(isFirstIn1Run)
+ 1)
  }
  > f(c(1,1,0,0), c(11,12,13,14))
    stretch start end no.of.1s
  1       1    11  12        2
  > f(c(1,1,0,1), c(11,12,13,14))
    stretch start end no.of.1s
  1       1    11  12        2
  2       2    14  14        1
  > f(c(0,1,0,1), c(11,12,13,14))
    stretch start end no.of.1s
  1       1    12  12        1
  2       2    14  14        1
  > f(c(0,1,0,0), c(11,12,13,14))
    stretch start end no.of.1s
  1       1    12  12        1
  > f(c(1,1,1,1), c(11,12,13,14))
    stretch start end no.of.1s
  1       1    11  14        4
  > f(c(0,0,0,0), c(11,12,13,14))
  [1] stretch  start    end      no.of.1s
  <0 rows> (or 0-length row.names)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com