On Sun, 2008-03-30 at 17:35 -0400, Daniel Malter wrote:> 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"))> a <- c("Alice","Alice","Alice","Alice","Bob","Bob") > tab <- table(a) > taba Alice Bob 4 2> which.max(tab)Alice 1> tab[which.max(tab)]Alice 4 HTH G> > > 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.-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
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
On 3/30/2008 5:35 PM, Daniel Malter wrote:> 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).> names(which.max(table(a))) [1] "Alice"> 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Thanks to all who responded so quickly. ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: Chuck Cleland [mailto:ccleland at optonline.net] Gesendet: Sunday, March 30, 2008 5:45 PM An: Daniel Malter Cc: r-help at stat.math.ethz.ch Betreff: Re: [R] maximum of identical elements in a vector On 3/30/2008 5:35 PM, Daniel Malter wrote:> 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 vectora.> 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).> names(which.max(table(a))) [1] "Alice"> 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894