I am trying to figure the mode of a data set, while displaying "NONE" if there is no mode. I am currently using Gregor's function as commented below.
examples:
{1,1,2,2,3}
Expected results 1 2
(success)
{NA,NA,NA,1,1,1,3,3}
Expected results NA 1
(success)
{1,2,3,4,5}
Expected result NONE
(success)
{1,1,1,1,1}
Expected result 1
(fail)
EDIT: I am trying out with if...and functions, as I realize there seems to be some logical problems. But I can't seem to crack it
smode<-function(x,...) {
ux <- unique(x)
tx <- tabulate(match(x, ux))
if(((unique(x)>=2)&(length(unique(tx)) == 1)) {
return("NONE")
}
max_tx <- tx == max(tx)
return(ux[max_tx])
}
Right now, {1,1,1,1,1,1} or any other alike just displays NONE
Just checked again, and {1,1,1,2,2,2} displays NONE
too instead of 1 2
I'm assuming it will be the same for more similiar things.
EDIT: I DID IT
function(x,...) {
ux <- unique(x)
tx <- tabulate(match(x, ux))
if(sum(tx)/length(tx) == 1) {
return("NONE")
}
max_tx <- tx == max(tx)
return(ux[max_tx])
}