knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Nigeria has 774 Local Government Areas (LGAs). There are a number of factors that can make working with them challenging:
The function fix_region
has methods that are designed to address spelling errors LGAs^[There is a method for repairing the names of States but the operation is more manageable since there are far fewer States]. These fixes can be carried out in 3 incremental phases; if the user cannot effect the corrections at a particular level s/he can proceed to the next stage. The phases are as follows:
When the spelling error is slight and unambiguous, the function automatically effects the repair. However, when there is only a single misspelt LGA, and especially if it is supplied as a string, fix_region
will signal an error.
library(naijR) fix_region("Legos Island")
The thinking is that an automated solution may not be necessary for a single value that was probably provided interactively.
When another LGA is added, such that the argument passed to the function is a character vector with more that one element:
fix_region(c("Legos Island", "Amuwo-Odofin"))
Sometimes when we use a character vector to perform this check, the function fix_region
may find it difficult to decide on what fixes to apply. In such instances, the best thing is to convert the vector into an lgas
object.
chars <- c("Legos Island", "Amuwo Odofin") fix_region(chars) # Create an `lgas` object lgs <- lgas(c("Legos Island", "Amuwo Odofin")) fix_region(lgs)
Note that the constructor lgas()
, by default, signals a warning when the vector we are supplying has misspelt LGAs. However, to avoid the unnecessary verbosity whilst attempting the fix, this warning is suppressed if lgas()
is nested in fix_region()
.
fix_region(lgas(c("Legos Island", "Amuwo Odofin")))
When the spelling mistakes depart far from the available LGA spellings, the user is shown a message stating what fixes could not be applied. To now carry out a fix, one can proceed to the next stage.
fix_region(lgas(c("Orange County", "Amuwo Odofin")))
When the automatic fix is not feasible, the user has the option of doing it interactively by calling fix_region
and setting its interactive
argument to TRUE
. By following the prompts, the misspelt LGA as well as possible replacements are presented. All the user needs to do is to select the desired replacement value. This is particularly useful when the user is not sure of what the correct spelling might be.
When a misspelt LGA has more than one match, the interactive approach is the viable option for effecting fixes. In the example below, not all the mispelt LGAs could be fixed automatically:
mispelt.adamawa <- c("Fufure", "Demsa", "Machika", "Fufure", "Ganye", "Hong") # check for misspelt LGAs and, if necessary, attempt to fix length(mispelt.adamawa) sum(is_lga(mispelt.adamawa)) corrected.adamawa <- fix_region(mispelt.adamawa) sum(is_lga(corrected.adamawa))
We see that the string 'Machika' is not an actual LGA and there are more than one possible candidate LGA that are considered as possible replacement. Our original intent was to use "Michika LGA". To address this mistake, we can run fix_region
interactively. Note that the interactive
option is only available for use with objects of class lgas
.
adamawa <- fix_region(lgas(corrected.adamawa), interactive = TRUE)
Next, the user is provided with a prompt to provide a search item for likely LGAs that would be an appropriate replacement for the misspelt one. Possible replacements are presented and to select any value, the appropriate number should be entered at the prompt. The option RETRY
allows the user to restart the query and provide another search term, SKIP
is to try out a different misspelt item (where there are more than one) and to QUIT
is to exit the prompt. On Windows machines, the user can use the native dialog boxes by setting graphic
to TRUE
.
adamawa <- fix_region_manual(corrected.adamawa, wrong = "Machika", correct = "Michika") adamawa
# Confirm that the spelling mistakes have been fixed. all(is_lga(adamawa))
When spelling errors are identified and the correct one is already known, then a manual fix can be applied. For this, we use the function fix_region_manual
and its use is straightforward^[Works for both character vectors and lgas
objects.]:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.