Last active
May 12, 2021 00:19
-
-
Save trinker/1f94189513705adf158c9bc1f6c08db8 to your computer and use it in GitHub Desktop.
generalized rescaling
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
general_rescale <- function(x, lower, upper){ | |
rng <- range(x, na.rm = TRUE, finite = TRUE) | |
if (diff(rng) == 0) return(stats::setNames(rep(upper, length(x)), names(x))) | |
(x - rng[1])/diff(rng) * diff(range(c(lower, upper))) + lower | |
} | |
x <- c(NA, 1:10) | |
general_rescale(x, 0, 1) | |
general_rescale(x, -1, 1) | |
general_rescale(x, 0, 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python Version