Last active
February 13, 2017 17:02
-
-
Save devloper13/55290742e3979cbf945154aaec3cd886 to your computer and use it in GitHub Desktop.
To calculate mode of numbers or characters in R.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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