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

Jeff Gentry jgentry at jimmy.harvard.edu
Thu Dec 19 17:33:12 CET 2002


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

-J




More information about the R-help mailing list