Skip to content

Instantly share code, notes, and snippets.

@danlewer
Created April 2, 2019 10:27
Show Gist options
  • Save danlewer/49291896ebf12e6d7fca782805d37122 to your computer and use it in GitHub Desktop.
Save danlewer/49291896ebf12e6d7fca782805d37122 to your computer and use it in GitHub Desktop.
# rescale a numeric vector to new maximum and minimum values
# original values will map to new values in a linear fashion
rescale <- function(x, MIN, MAX) { # linear rescaling
y <- x - min(x)
y <- y * (MAX - MIN) / max(y)
y + MIN
}
# example
rescale(1:10, 0, 100)
rescale(1:10, 100, 0) # also works backwards
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment