Created
November 29, 2019 18:09
-
-
Save JosepER/11ed48c54a025de44230de82fb903a5b to your computer and use it in GitHub Desktop.
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(x, tie = c("missing", "error"), ignore_categories = NULL) { | |
if(!is.null(ignore_categories)){ | |
x <- x[!(x %in% ignore_categories)] | |
} | |
ux <- unique(x) | |
tab <- tabulate(match(x, ux)) | |
output <- ux[tab == max(tab)] | |
if(length(output) == 1){ | |
return(output) | |
}else if(tie == "missing"){ | |
return(NA) | |
}else if(tie == "error"){ | |
stop("There are multiple modes, and 'tie' argument was set to error.") | |
}else if(tie == "min"){ | |
return(min(output)) | |
}else if(tie == "first"){ | |
return(output[1]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment