[R] uniq -c

Marc Schwartz marc_schwartz at comcast.net
Wed Nov 21 14:11:39 CET 2007


On Wed, 2007-11-21 at 15:27 +0300, Alexy Khrabrov wrote:
> Is there an R analog of the Unix command uniq -c:
> 
> http://en.wikipedia.org/wiki/Uniq
> 
> Given an array x, uniq -c replaces each contiguous subsequence of  
> identical numbers with a tuple (count, number).  E.g.
> 
> $ cat > usample
> 10
> 10
> 9
> 8
> 8
> 7
> 7
> 7
> 6
> 3
> 1
> 1
> 1
> 0
> $ uniq -c usample
>        2 10
>        1 9
>        2 8
>        3 7
>        1 6
>        1 3
>        3 1
>        1 0
> 
> Cheers,
> Alexy

Alexy,

See ?rle

> Vec
 [1] 10 10  9  8  8  7  7  7  6  3  1  1  1  0

> rle(Vec)
Run Length Encoding
  lengths: int [1:8] 2 1 2 3 1 1 3 1
  values : num [1:8] 10 9 8 7 6 3 1 0

HTH,

Marc Schwartz



More information about the R-help mailing list