[R] counting entries in vector

Stavros Macrakis macrakis at alum.mit.edu
Wed Feb 4 16:33:34 CET 2009


Take a look at the run-length encoding function rle.  I believe
rle(k)$lengths gives you exactly what you want.

            -s

On Wed, Feb 4, 2009 at 10:19 AM, axionator <axionator at gmail.com> wrote:
> Hi all,
> I've a vector with entries, which are all of the same type, e.g. string:
> k <- c("bb", "bb", "bb", "aa", "cc", "cc")
> and want to create a second vector containing the number of each entry
> in k in the same order as in k, i.e.
> c(3, 1, 2)
>
> or:
> k <- c(5,5,5,5,2,2,4)
> => c(4,2,1)
>
> thanks
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list