Skip to content

Instantly share code, notes, and snippets.

@jlehtoma
Created August 22, 2012 12:59
Show Gist options
  • Save jlehtoma/3425358 to your computer and use it in GitHub Desktop.
Save jlehtoma/3425358 to your computer and use it in GitHub Desktop.
Standardize value ranks between [0, 1]
# Based on:
# http://stackoverflow.com/questions/4401498/how-to-rank-values-in-a-vector-and-give-them-corresponding-values
# http://stackoverflow.com/questions/5468280/scale-a-series-between-two-points-in-r/5468527#5468527
values <- runif(1000000)
# replace runif with getValues(raster)
ranks <- as.numeric(factor(values))
# Transform variables to a scale between 0 and 1, while retaining rank order and
# the relative size of separation between values.
range01 <- function(x) {
return((x-min(x)) / (max(x) - min(x)))
}
stand.ranks <- range01(ranks)
# Check the difference between elements in stand.ranks
diff(sort(stand.ranks))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment