[R] Consecutive zeros in a vector

From: Kenn Konstabel <lebatsnok_at_gmail.com>
Date: Tue, 29 Apr 2008 18:39:00 +0300


Suppose X is a long vector of integers (typically about 30000 elements). Is there an efficient way to detect whether there are at least N consecutive zeros in X, and if yes, where does this occur?

for example, suppose X is:
  1 2 3 4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 2 0 1 2 0 0 0 2 2 2 2 2 ...

and N is 10. I would like a foo(X, 10) to return something like c(6,19) (start and end positions in X of at least 10 consecutive zeros). I can think of a number of slow and probably inefficient ways of doing it (nested loops..), but perhaps someone has a better idea..

Thanks in advance,
Kenn

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 29 Apr 2008 - 17:14:21 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Apr 2008 - 18:30:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive