Skip to content

Instantly share code, notes, and snippets.

@devloper13
devloper13 / mode.r
Last active February 13, 2017 17:02
To calculate mode of numbers or characters in R.
mode<-function(v){
occur<-table(v) #Creates a table with unique numbers as labels (ascending order) and occurrences in the first row.
boolean<-max(occur) == occur #max(v) extracts the maximum number from 'occur' and matches with the original set to give boolean values
value<-names(occur)[boolean] #Searches for label having 'TRUE'
return as.numeric(value)
}