Skip to content

Instantly share code, notes, and snippets.

@devloper13
Last active February 13, 2017 17:02
Show Gist options
  • Save devloper13/55290742e3979cbf945154aaec3cd886 to your computer and use it in GitHub Desktop.
Save devloper13/55290742e3979cbf945154aaec3cd886 to your computer and use it in GitHub Desktop.
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment