R/codeNameQuery.R

Defines functions codeNameQuery

Documented in codeNameQuery

#' Code or Name Lookup
#'
#' Gets corresponding code or name, given the other.
#' @param code input character vector of name or code
#' @param flag Either string name or string number
#' @return character string of code or name
#' @export
#' @examples
#' codeNameQuery("Kathmandu", "name")
#' codeNameQuery("524 3 09 45", "number")

codeNameQuery <- function(code, flag) {
  codefix_hrrp <- read.csv("/Users/bradleywilson/dev/nepal/codefix_hrrp.csv",
                           header = TRUE, sep = ",")
  code.length <- nchar(code) # Checks if dist or vdc code

  if (flag == "name") {
    # Name to Code conversions
    if (code %in% codefix_hrrp$HRRP_VNAME == TRUE) {
      # VDCS
      ind.name <- match(code, codefix_hrrp$HRRP_VNAME)
      return(as.character(codefix_hrrp$HRRP_VCODE[[ind.name]]))
    } 
    else if (code %in% codefix_hrrp$HRRP_DNAME == TRUE) {
      # Districts
      ind.name <- match(code, codefix_hrrp$HRRP_DNAME)
      return(as.character(codefix_hrrp$HRRP_DCODE[[ind.name]]))
    }
    else # Failed input
      print("Wrong name, check spelling?")
  }

  else {
    # Code to Name converstions
    if (code.length == 17) {
      # VDCS
      ind.name <- match(code, codefix_hrrp$HRRP_VCODE)
      return(as.character(codefix_hrrp$HRRP_VNAME[[ind.name]]))
    } 
    else if (code.length == 11) {
      # Districts
      ind.name <- match(code, codefix_hrrp$HRRP_DCODE)
      return(as.character(codefix_hrrp$HRRP_DNAME[[ind.name]]))
    }
    else # Failed input
      print("Wrong code")
  }
}
bradleyswilson/hrrp documentation built on May 28, 2019, 7:13 p.m.