Re: [R] Need help locating the longest series of consecutive numbers in a matrix

2009-10-28 Thread jim holtman
Use 'rle': > x <- rle(sample[,2]) > x Run Length Encoding lengths: int [1:9] 10 3 3 1 1 1 1 17 3 values : num [1:9] 0 1 0 1 0 1 0 1 0 > which.max(x$lengths[x$values==1]) [1] 4 > which.max(x$lengths * x$values) # makes use of the fact you are only using 0 > & 1 [1] 8 > cumsum(c(1, x$lengths))

[R] Need help locating the longest series of consecutive numbers in a matrix

2009-10-28 Thread Staples, Angela Dawn
I need to determine the length of the longest series of consecutive numbers (1's to be specific) and the start time of that series. For example, in the following sample, the first column is "time" and the second column indicates the presence of the target behavior. I would like a function that wou