[R] R function similar to UNIX "uniq -c"?

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Thu Dec 19 17:41:02 CET 2002


How about

> table(factor(z))

a b c d
3 1 2 1

?


On Thu, 19 Dec 2002, Jeff Gentry wrote:

> On Thu, 19 Dec 2002, Hilmar M. Carders wrote:
> > Is there an R function that gives the equivalent of the UNIX command
> > "uniq -c" which could be applied to a vector with duplicate values?
>
> There must be a better way to do this, but this would get you the
> information you're looking for:
>
> > z <- c("a","b","c","d","a","a","c")
> > unique(z)
> [1] "a" "b" "c" "d"
> > lapply(unique(z),function(x){length(z[z==x])})
> [[1]]
> [1] 3
>
> [[2]]
> [1] 1
>
> [[3]]
> [1] 2
>
> [[4]]
> [1] 1

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list