-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
primeira versao de padronizar_ceps()
- Loading branch information
Showing
6 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(padronizar_ceps) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#' Padronizar CEPs | ||
#' | ||
#' Padroniza CEPs. | ||
#' | ||
#' @param ceps Um vetor de caracteres ou números. Os CEPs a serem padronizados. | ||
#' | ||
#' @return Um vetor de caracteres com os CEPs padronizados. | ||
#' | ||
#' @examples | ||
#' ceps <- c("22290-140", "22.290-140", "22290 140", "22290140") | ||
#' padronizar_ceps(ceps) | ||
#' | ||
#' ceps <- c(22290140, 1000000, NA) | ||
#' padronizar_ceps(ceps) | ||
#' | ||
#' @export | ||
padronizar_ceps <- function(ceps) { | ||
checkmate::assert( | ||
checkmate::check_character(ceps), | ||
checkmate::check_numeric(ceps), | ||
combine = "or" | ||
) | ||
|
||
# alguns ceps podem vir vazios e devem permanecer vazios ao final. nesse caso, | ||
# a chamada da str_pad() abaixo faz com que esses ceps virem "00000000". para | ||
# evitar que o resultado contenha esses valores, identificamos o indice dos | ||
# ceps vazios para ao final "reesvazia-los" | ||
|
||
indice_cep_vazio <- which(ceps == "" | is.na(ceps)) | ||
|
||
ceps_padrao <- if (is.numeric(ceps)) { | ||
format(ceps, scientific = FALSE) | ||
} else { | ||
ceps | ||
} | ||
|
||
ceps_padrao <- stringr::str_squish(ceps_padrao) | ||
ceps_padrao <- stringr::str_replace_all(ceps_padrao, "\\.|-|,| ", "") | ||
ceps_padrao <- stringr::str_pad(ceps_padrao, width = 8, pad = "0") | ||
|
||
ceps_padrao[indice_cep_vazio] <- "" | ||
|
||
return(ceps_padrao) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
reference: | ||
- title: "Campos individuais" | ||
- contents: | ||
- padronizar_ceps | ||
|
||
development: | ||
mode: auto |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.