Skip to content
Prev 315346 / 398503 Next

Code to fetch summary info from vector

I don't completely understand the  question, but if you are looking
for the lengths of the runs of values greater than 1 then rle() would
help:
  > b <- c(1,1,1,2,3,4,3,2,1,1,1,1,1,2,3,4,5,4,3.5,3,2,1,1,1)
  > r <- rle(b>1)
  > r
  Run Length Encoding
    lengths: int [1:5] 3 5 5 8 3
    values : logi [1:5] FALSE TRUE FALSE TRUE FALSE
  > r$lengths[r$values]
  [1] 5 8
To get the maximum of each run of values greater than one, something
like the following may do (there are more elegant ways, but I don't want
to spend the time on it until I know what the question is):
  > runNumber <- cumsum( c(b[1]>1, (b[-1]>1) & (b[-length(b)]<=1)) )
  > runNumber[b<=1] <- NA
  > tapply(b, runNumber, max)
  1 2 
  4 5

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com