[R] How to group by then count?

Christian Brandstätter bran.chri at gmail.com
Sun Jan 4 11:17:59 CET 2015

Dear Monnad,

one possible way would be to use as.factor() and in the summary you would get counts for every level.

Like this:

  x = c("1", "1", "2", "1", "5", "2")


Cheers, Christian

> Hi all,
> I thought this was a very naive problem but I have not found any solution
> which is idiomatic to R.
> The problem is like this:
> Assuming we have vector of strings:
>   x = c("1", "1", "2", "1", "5", "2")
> We want to count number of appearance of each string. i.e. in vector x,
> string "1" appears 3 times; "2" appears twice and "5" appears once. Then I
> want to know which string is the majority. In this case, it is "1".
> For imperative languages like C, C++ Java and python, I would use a hash
> table to count each strings where keys are the strings and values are the
> number of appearance. For functional languages like clojure, there're
> higher order functions like group-by.
> However, for R, I can hardly find a good solution to this simple problem. I
> found a hash package, which implements hash table. However, installing a
> package simple for a hash table is really annoying for me. I did find
> aggregate and other functions which operates on data frames. But in my
> case, it is a simple vector. Converting it to a data frame may be not
> desirable. (Or is it?)
> Could anyone suggest me an idiomatic way of doing such job in R? I would be
> appreciate for your help!
> -Monnand
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

	[[alternative HTML version deleted]]

More information about the R-help mailing list