[R] Histogram of gaps

Chuck Cleland ccleland at optonline.net
Tue Jun 10 15:44:32 CEST 2008


On 6/10/2008 9:23 AM, Creighton, Sean wrote:
> Hi
> 
> I consider myself not a complete beginner in R, however an elegant
> solution to this problem stumps me. I have a fairly long time series of
> 60000 or so points, I need to gather the data to create a histogram of
> the length of continuous zero periods in a data set. 
> 
> So image the data looks like this (where as a pre-condition all numbers
> are single digit so assume commas if you which, I have the time series
> properly formatted in a zoo object)
> 
> 333343245352352352353252335000003343253532523523522352352353253225325300
> 002352352332552533252353200325235235
> 
> I should get the following zero periods out: 
> 
> 333343245352352352353252335 <00000>
> 33432535325235235223523523532532253253 <0000> 23523523325525332523532
> <00> 325235235
> 
> So I have three entries for my histogram 5,4 and 2. Now can anyone give
> me any tips that doesn't involve using a loop which will be quite time
> consuming? Is there some function in a libray that already does
> something like this?

   How about rle()?  For example:

X <- sample(0:9, 60000, replace=TRUE)

table(rle(X == 0)[[1]][rle(X == 0)[[2]]])

    1    2    3    4
4813  490   49    3

hist(rle(X == 0)[[1]][rle(X == 0)[[2]]])

?rle

> Thanks
> Sean
> 
> ______________________________________________
> 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. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list