Re: [R] a more elegant approach to getting the majority level

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Fri 27 May 2005 - 01:00:23 EST

you could try this:

x <- factor(sample(letters[1:3], 20, TRUE)) #######
tab <- table(x)
names(tab)[tab == max(tab)]

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


> Hi, I have a factor and I would like to find the most frequent
> level.
>
> I think my current approach is a bit long winded and I was wondering
> if
> there was a more elegant way to do it:
>
> x <- factor(sample(1:0, 5,replace=TRUE))
>
> levels(x)[ which( as.logical((table(x) == max(table(x)))) ==
> TRUE ) ]
>
> (The length of x will always be an odd number, so I wont get a tie
> in
> max())
>
> Thanks,
>
> -------------------------------------------------------------------
> Rajarshi Guha <rxg218_at_psu.edu> <http://jijo.cjb.net>
> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
> -------------------------------------------------------------------
> Alcohol, an alternative to your self
> - 'Alcohol' by the Bare Naked Ladies
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri May 27 01:18:51 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:07 EST