Skip to content

Instantly share code, notes, and snippets.

@shamilnabiyev
Last active June 1, 2019 23:49
Show Gist options
  • Save shamilnabiyev/f5bf0a809d5288d980caf41134a1de4c to your computer and use it in GitHub Desktop.
Save shamilnabiyev/f5bf0a809d5288d980caf41134a1de4c to your computer and use it in GitHub Desktop.
Rescaling (min-max normalization) in R
age <- c(0, 2, 3, 5, 7, 10)
income <- c(0, 450, 900, 1200, 3000, 50000)
scale <- function(val, min_, max_) {val - min_}/(max_ - min_)
sapply(age, scale, min(age), max(age))
# Output: 0.0 0.2 0.3 0.5 0.7 1.0
sapply(income, scale, min(income), max(income))
# Output: 0.000 0.009 0.018 0.024 0.060 1.000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment