Skip to content

Mode in case of discrete or categorial data

3 messages · Vito Ricci, Thomas Lumley, John Fox

#
Thanking John for his suggestion I build this function
which get the mode of both categorial and discrete
data.


Mode<-function(x){t<-table(x)
if (is.numeric(x)) as.numeric(names(t)[t == max(t)])
else (names(t)[t == max(t)])
}

Any other improvement and suggestion will welcome.

Best

Vito
[1] 1 1 6 1 1 7 6 5 6 2 1 4 5 6 6 7 3 5 4 1 7 3 7 3 3
7 7 2 1 4 4 2 7 7 6 6 1 2
[39] 5 1 7 7 5 5 7 3 5 6 5 6 3 6 6 4 2 1 5 3 3 3 6 5 2
4 3 2 2 1 5 3 4 3 1 3 3
[1] 3
[1] "C" "A" "C" "D" "B" "A" "B" "B" "B" "A" "D" "D"
"A" "D" "D" "A" "D" "C" "B"
[20] "D" "C" "B" "D" "C" "B" "C" "D" "A" "C" "A" "A"
"A" "C" "A" "D" "A" "B" "B"
[39] "A" "B"
[1] "A"


=====
Diventare costruttori di soluzioni
Became solutions' constructors

"The business of the statistician is to catalyze 
the scientific learning process."  
George E. P. Box


Visitate il portale http://www.modugno.it/
e in particolare la sezione su Palese http://www.modugno.it/archivio/cat_palese.shtml
#
On Fri, 12 Nov 2004, Vito Ricci wrote:
which.max is design for finding the maximum, so
    names(t)[which.max(t)]


 	-thomas
#
Dear Thomas,

I believe that which.max() will report only the first maximum in case of
ties [which is why I suggested the more awkward t == max(t)].

Regards,
 John

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
--------------------------------