Longest string of ones

Goal: find the longest string of 1's in a vector.

Input:

## Initial input
x <- c(1, 1, 3, 6, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 3)
y <- c(1, 1, 2, 3, 4, 1, 1, 1, 1, 1, 6, 1, 1, 1, 2)

## Group data
data <- list(x, y)
data
## [[1]]
##  [1] 1 1 3 6 1 1 1 1 1 1 1 3 1 1 3
## 
## [[2]]
##  [1] 1 1 2 3 4 1 1 1 1 1 6 1 1 1 2

Expected output:

Doing it with Rle's

## Load pkgs
suppressMessages(library(IRanges))

## Transform to Rle's
data.rle <- lapply(data, Rle)
data.rle
## [[1]]
## numeric-Rle of length 15 with 7 runs
##   Lengths: 2 1 1 7 1 2 1
##   Values : 1 3 6 1 3 1 3
## 
## [[2]]
## numeric-Rle of length 15 with 8 runs
##   Lengths: 2 1 1 1 5 1 3 1
##   Values : 1 2 3 4 1 6 1 2

## Find which runs are of the value 1
ones <- lapply(data.rle, function(x) {
    which(runValue(x) == 1)
})
ones
## [[1]]
## [1] 1 4 6
## 
## [[2]]
## [1] 1 5 7

## Function for finding the longest string of 1's from an Rle
getMax <- function(rle, one) {
    max(runLength(rle)[one])
}

## Finally, find the length of the longest string of 1's
result <- mapply(getMax, data.rle, ones)
result
## [1] 7 5

Reproducibility

sessionInfo()
## R version 3.0.1 (2013-05-16)
## Platform: x86_64-apple-darwin10.8.0 (64-bit)
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] parallel  stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] IRanges_1.18.2     BiocGenerics_0.6.0 knitr_1.3         
## 
## loaded via a namespace (and not attached):
## [1] digest_0.6.3   evaluate_0.4.4 formatR_0.8    stats4_3.0.1  
## [5] stringr_0.6.2  tools_3.0.1
proc.time()
##    user  system elapsed 
##   2.347   0.059   2.402