-
-
Save leoschet/e6642109b203a1f84032 to your computer and use it in GitHub Desktop.
R code (mode calculator).
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
# author: ljsa @ cin.ufpe.br | |
# dataset = vector of numbers | |
mode = function (dataset) { | |
dataset = sort(dataset); | |
maxTam = 1; | |
curTam = 0; | |
counter = 2; | |
lastMode = dataset[1]; | |
lastData = lastMode; | |
md = c(lastMode); | |
for (i in 2:length(dataset)) { | |
data = dataset[i]; | |
if(data == lastMode){ | |
maxTam = maxTam + 1; | |
} else if (data != lastMode){ | |
if (lastData != data){ | |
curTam = 0; | |
lastData = data; | |
} | |
curTam = curTam + 1; | |
if (curTam > maxTam){ | |
md = c(data); | |
maxTam = curTam; | |
curTam = 0; | |
lastMode = data; | |
} else if(curTam == maxTam){ | |
md = c(md, data); | |
lastMode = data | |
} | |
} | |
} | |
return (md); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment