[R] maximum of identical elements in a vector

From: Daniel Malter <daniel_at_umd.edu>
Date: Sun, 30 Mar 2008 17:35:00 -0400


Hi,

the problem I have is seemingly very simple, but not simple enough for me to figure out. I just want to find the most (or least) frequent element in the vector.

a=c("Alice","Alice","Alice","Alice","Bob","Bob") unique(a)
length(which(a=="Alice"))

unique(a) shows me that the elements in my vector are "Alice" and "Bob". The latter expression gives 4 as the frequency of "Alice" in vector a. To find the maximum frequency of the unique elements, however, it assumes that I know that "Alice" is the most (or least) frequent element. Therefore, how can find that "Alice" is the most frequent element in this vector? I assume there is an easier way than computationally intensive loops (for long vectors).

Cheers,
Daniel



cuncta stricte discussurus

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. Received on Sun 30 Mar 2008 - 21:37:50 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 30 Mar 2008 - 22:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive