[R] uniq -c

Charles C. Berry cberry at tajo.ucsd.edu
Wed Nov 21 18:51:36 CET 2007


A further tip:

 	subset(as.data.frame(xtabs( ~. , dat )), Freq != 0 )

Comes very close to generating output along the lines of what

 	'sort | uniq -c'

provides (if this is what was wanted rather than rle() ) and works for 
multiple columns of data. The count becomes the last column (labelled 
'Freq'); it is trivial to reorder columns if needed.

HTH,

Chuck

On Wed, 21 Nov 2007, Henrique Dallazuanna wrote:

> See ?table function.
>
> On 21/11/2007, Alexy Khrabrov <deliverable at gmail.com> 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
>>
>> ______________________________________________
>> 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.
>>
>
>
> -- 
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list