[R] How to generate a vector of counts i.e. the number of rows that are contiguous

Gabor Grothendieck ggrothendieck at gmail.com
Tue Mar 11 16:03:55 CET 2008


See ?diff and ?which:

> x <- c(18004, 18005, 18006, 18007, 18008, 18009, 18010, 18011,
+ 18012, 18013, 18014, 18017, 18060, 18061, 18062, 18063,
+ 18064, 18065, 18066)
> diff(which(diff(c(-Inf, x, Inf)) != 1))
[1] 11  1  7
>


On Tue, Mar 11, 2008 at 10:37 AM, Ng Stanley <stanleyngkl at gmail.com> wrote:
> Hi,
>
> I have a 3 columns data and need to generate a vector of counts. These
> counts are the number of rows that are contiguous. It looks easy for manual
> counting, but to turn it algorithmic really split my head.
>
> For example, 11 (i.e. 18004 to 180014), 1 (i.e., 18017), 7 (i.e., 18060 to
> 18064)
>
> 18004  10314568 0.5205757
> 18005  10333837 0.5135572
> 18006  10566333 0.5111271
> 18007  10566624 0.8196611
> 18008  10566629 0.8172404
> 18009  10567318 0.8148244
> 18010  11846564 0.8151912
> 18011  12308703 0.8151912
> 18012  12937846 0.6670504
> 18013  13318038 0.6733384
> 18014  13657045 0.6656946
> 18017  16200749 0.6173948
> 18060  23614276 0.7754780
> 18061  23676384 0.7781018
> 18062  23752656 0.7584247
> 18063  23804792 0.7389500
> 18064  23917628 0.7197741
> 18065  24397834 0.7008880
> 18066  24877926 0.6822831
>
>        [[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.
>



More information about the R-help mailing list