R/pedis-helper.R

#' Replace duplicated commas
#'
#' @param x A string
#'
#' @return A numeric object
#'
replace_duplicated_commas <- function(x) {
  x <- gsub(x = x, pattern = "(\\d+)\\,(\\d+)\\,(\\d+)", replacement = "\\1.\\2\\3")
  as.numeric(x)
}

#' Replace whitespaces and non numeric alpha characters
#'
#' Used to process the columnnames in the raw data excel files.
#' Used in the `read_pedis` function
#'
#' @param string A string
#'
#' @import stringr
#'
#' @return A single string or a character vector without spaces and only alpha numerical characters
#'
replace_space_nonnumalpha <- function(string) {
  string <- stringr::str_replace_all(string = string, pattern = '[[:punct:]]|\\s', replacement = "_")
  string <- stringr::str_replace_all(string = string, pattern = '_{2,}', replacement = '_')
  string <- stringr::str_replace_all(string = string, pattern = '_$', replacement = '')
  string <- stringr::str_replace_all(string = string, pattern = '_1$', replacement = '')
  string <- stringr::str_replace_all(string = string, pattern = '^$', replacement = 'x')
  string <- tolower(string)
  return(string)
}

#' Discretize continous metric extent variable.
#'
#' @param x A numeric vector
#' @param cutoff A factor vector
#'
#' @return A vector
#'
discrete_extent <- function(x, cutoff = 5) {
  case_when(
    x <= 1 ~ 1L,
    x <  cutoff ~ 2L,
    x >=  cutoff ~ 3L,
    TRUE ~ as.integer(NA)
  )
}
jnshsrs/PEDISdata documentation built on June 24, 2019, 12:07 p.m.