[R] Modes again

Gregor GORJANC gregor.gorjanc at bfro.uni-lj.si
Fri Nov 26 11:25:11 CET 2004


I write once more. I slightly modified your code Ross and now it returns 
all modes from the distribution. I changed the name of the funtion to 
distinguish the difference. I do not like the name simpleMode, however 
mode is already taken by another function (The (Storage) Mode of an Object).

It would be nice to have some additional options for number of 
observations in modes, however this is above my current programming 
skills in R. I don't like the use of cat for returning the result. That 
is the reason I am sending this mail also to R-devel list. Maybe anybody 
there can solve the issue. Quite some users asked for mode calculation 
function and it would really nice to have it in R by default.

modus <- function (x) {

     # Returns the modes of a value set
     # Based on Ross NELSONs code from 2004-11-16 (on R-help mailling list)

     # create a table with the values sorted by frequency
     sortvar <- sort(table(x))
     # get the max frequency
     maxfreq <- max(sortvar)
     # find values with high frequency
     modeTable <- which(sortvar == maxfreq)
     # get the name (value) of the most frequent value
     themoderesult <- names(modeTable)
     # Report

     # data(CO2)
     # modus(CO2$uptake)
     # rm(CO2)


Lep pozdrav / With regards / Con respeto,
     Gregor GORJANC

University of Ljubljana
Biotechnical Faculty       URI: http://www.bfro.uni-lj.si
Zootechnical Department    mail: gregor.gorjanc <at> bfro.uni-lj.si
Groblje 3                  tel: +386 (0)1 72 17 861
SI-1230 Domzale            fax: +386 (0)1 72 41 005

More information about the R-help mailing list