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

Eric Lecoutre lecoutre at stat.ucl.ac.be
Thu May 26 17:00:27 CEST 2005

You could also use:

> names(rev(sort(table(x))))[1]

There is nonetheless a difference if there are several levels which
provides this maximum.
This method will only return one, yours would return all those levels
(which may not be desirable for some others processing).



Eric Lecoutre
UCL /  Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve

tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be

If the statistics are boring, then you've got the wrong numbers. -Edward

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Rajarshi Guha
> Sent: jeudi 26 mai 2005 16:37
> To: R
> Subject: [R] a more elegant approach to getting the majority level
> 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 at 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

More information about the R-help mailing list