Last active
February 20, 2018 10:34
-
-
Save m-Py/0ff246b03444a75c636f52c48de7bff1 to your computer and use it in GitHub Desktop.
Compute ordinal scores from continuous data
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 Martin Papenberg | |
## Year 2018 | |
## This code is released into the public domain. Anybody may use, alter | |
## and distribute the code without restriction. The author makes no | |
## guarantees, and takes no liability of any kind for use of this code. | |
#' Compute ordinal scores from continuous data | |
#' | |
#' Might be useful for data exploration with highly skewed data | |
#' (e.g. having outliers). As this leads to a loss in information, | |
#' it should be used with caution. | |
#' | |
#' @param x A numeric vector | |
#' @param groups How many different scores should be computed (2 leads | |
#' to a median split). | |
#' @param na.rm logical; if true, any ‘NA’ and ‘NaN’'s are removed from | |
#' ‘x’ before the scores are computed. | |
#' | |
#' @return A vector a of length `length(x)` where each data point | |
#' represents the ordinal score. | |
ordinal_scores <- function(x, groups, na.rm = FALSE) { | |
probs <- seq(0, 1, length.out=groups+1) | |
quants <- quantile(x, probs, na.rm) | |
quant_scores <- vector(length=length(x)) | |
for (i in length(quants):2) { | |
quant_scores <- ifelse(x <= quants[i], i, quant_scores) | |
} | |
return(quant_scores-1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment