Skip to content

Commit

Permalink
improves the overall structure and functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
YaoxiangLi committed Feb 23, 2024
1 parent e120e15 commit 74d1273
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 31 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gendercoder
Title: Recodes Sex/Gender Descriptions into a Standard Set
Version: 0.0.0.9000
Version: 0.1.0
Authors@R: c(
person("Yaoxiang", "Li", role = c("aut", "cre"),
email = "[email protected]",
Expand Down
35 changes: 12 additions & 23 deletions R/recode_gender.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
which_is_na <- function(x) {
which(is.na(names(x)))
}

#' recode_gender
#'
#' \code{recode_gender} matches uncleaned gender responses to cleaned list using
Expand Down Expand Up @@ -31,34 +27,27 @@ which_is_na <- function(x) {
#' ))
#'
#' @export recode_gender

recode_gender <- function(gender = gender,
dictionary = gendercoder::manylevels_en,
retain_unmatched = FALSE) {
if (class(dictionary) != "character") {
stop("The supplied dictionary is not a character vector")
if (!is.vector(dictionary) || class(dictionary) != "character") {
stop("The supplied dictionary must be a character vector.")
}

dictionary <- tolower(dictionary)
names(dictionary) <- tolower(names(dictionary))
dictionary <- dictionary[!duplicated(names(dictionary))]

# remove duplicates (if any) from dictionary
dictionary <- dictionary[!duplicated(names(dictionary), fromLast = TRUE)]
gender_lower <- tolower(trimws(gender))
recoded <- dictionary[gender_lower]

# match using supplied dictionary
recoded <- dictionary[tolower(trimws(gender))]

# replace missing values with inputs
if (retain_unmatched == TRUE & length(gender[which_is_na(recoded)]) > 0) {
message(
paste(
# length(gender[which_is_na(recoded)]),
"Results not matched from the dictionary have been filled",
"with the user inputted values"
)
)

recoded[is.na(recoded)] <- gender[is.na(recoded)]
if (retain_unmatched) {
unmatched_indices <- which(is.na(recoded))
if (length(unmatched_indices) > 0) {
message("Some results not matched from the dictionary have been filled with the original values.")
recoded[unmatched_indices] <- gender[unmatched_indices]
}
}

unname(recoded)
}
7 changes: 0 additions & 7 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ other gender-diverse responses to the `manylevels_en` and `fewlevels_en` diction

The ["Adding to the dictionary"](https://ropensci.github.io/gendercoder/articles/a02_add_dictionary.html) vignette includes information about how to make changes to the dictionary either for your own use or when contributiong to the gendercoder package.

## Citation Information

Please cite this package as:

Jennifer Beaudry, Emily Kothe, Felix Singleton Thorn, Rhydwyn McGuire, Nicholas Tierney and Mathew Ling (2020).
gendercoder: Recodes Sex/Gender Descriptions into a Standard Set. R package version 0.0.0.9000.
https://github.com/ropensci/gendercoder

## Acknowledgement of Country

Expand Down

0 comments on commit 74d1273

Please sign in to comment.