Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.SOL.4.20.0212191128570.3318-100000@santiam.dfci.harvard.edu>
Date: 2002-12-19T17:33:12Z
From: Jeff Gentry
Subject: R function similar to UNIX "uniq -c"?
In-Reply-To: <Pine.LNX.4.44.0212191025100.21516-100000@north.sr.unh.edu>

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