Skip to content

Instantly share code, notes, and snippets.

@graebnerc
Created March 24, 2022 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graebnerc/a7d1b9e02c761b7ad7ac553d760c9c5d to your computer and use it in GitHub Desktop.
Save graebnerc/a7d1b9e02c761b7ad7ac553d760c9c5d to your computer and use it in GitHub Desktop.
Solution to the exercise of session 3 in the Data Science course at the EUF. The task was to write a function that normalizes numeric vectors into the range of zero and one.
#' Normalize a numeric vector into zero-one-range
#'
#' This simple function covers simple cases: when you have a vector of numbers
#' larger or equal to zero, then this function normalizes these numbers into
#' the range between zero and one.
#' @param input_vector A vector of numeric values greater or equal to zero
#' @return A vector of the same length as `input_vector`, the elements are
#' normalized into the range between zero and one
normalize_vector <- function(input_vector){
min_max_diff <- max(input_vector) - min(input_vector)
diff_vec <- input_vector - min(input_vector)
normalized_vector <- diff_vec / min_max_diff
return(normalized_vector)
}
normalize_vector(input_vector = c(1,2,3,4))
normalize_vector(input_vector = c(2, 5, 10, 1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment