SHARE
TWEET

Custom mode function

a guest Sep 17th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. moreThanOneModeData <- c(1,1,1,2,2,2,3,4,5,6,7,8)
  2. zeroModesData <- c(1,2,3,4,5,6)
  3. testData <- c(1,2,2,3,4,5,6)
  4.  
  5. modeFunction <- function(x) {
  6.   ux <- unique(x)
  7.   tab <- tabulate(match(x, ux))
  8.   result <- ux[tab == max(tab)]
  9.   if (length(result) == 1) {
  10.     return(result)
  11.   } else {
  12.     return("There is no single mode")
  13.   }
  14.   return(result)
  15. }
  16.  
  17.  
  18. modeFunction(moreThanOneModeData)
  19. modeFunction(zeroModesData)
  20. modeFunction(testData)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top