Created
March 24, 2022 15:11
-
-
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.
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
#' 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