R/add_values_to_key.R

Defines functions add_values_to_key

Documented in add_values_to_key

#' Add Values to Key
#'
#' Adds values to an encoding key as generated by `make_encoding_key()`.
#'
#' @param values_to_add A vector of values to add to key.
#' @param prime_key The old keys to which values are to be added.
#'
#' @usage add_values_to_key(values_to_add, prime_key)
#'
#' @return A vector containing a new key, including both old and new values.
#' @export
#' @seealso [primr::make_encoding_key()]
#' @md
#'
#' @examples
#' # Add "Arthropod" to encoding key.
#' new_prime_key <- add_values_to_key("Arthropod", c(Fish = 2, Mammal = 3, Bird = 5))
add_values_to_key <- function(values_to_add, prime_key) {

  # Check whethee any new values are in values_to_add, compared to prime_key.
  if (sum(!values_to_add %in% names(prime_key)) == 0) {
    warning("All values in argument values_to_add already exist in argument prime_key. No new values were added.")
    return(prime_key)
  }

  # Generate a new list of primes.
  old_and_new_primes <- primr::generate_primes(length(c(names(prime_key), unique(values_to_add)[!unique(values_to_add) %in% names(prime_key)])))

  # Remove existing primes.
  new_primes <- old_and_new_primes[!(old_and_new_primes %in% prime_key)]

  # Assign names to new primes.
  names(new_primes) <- unique(values_to_add)[!unique(values_to_add) %in% names(prime_key)]

  # Merge old and new encoding keys.
  new_prime_key <- c(prime_key, new_primes)

  return(new_prime_key)
}
JonasEngstrom/primr documentation built on June 9, 2022, 9:43 p.m.