Skip to content

Commit

Permalink
feat: clean binomial names
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Mar 16, 2023
1 parent 019d1ad commit 55c22e5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export(get_phylopic_image)
export(get_world_basemap)
export(leading_zero)
export(multi_merge)
export(to_binomial_name)
45 changes: 45 additions & 0 deletions R/to_binomial_name.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' Correct species binomial name case
#'
#' @description
#' Corrects species binomial name case.
#'
#' @param x a `character` vector with misspelled binomial names.
#'
#' @return A `character` vector of same length as `x`.
#'
#' @export
#'
#' @examples
#' x <- c("sAlMO saLAR (Linnee, 1765)", "tyranosurus_REX rex.1")
#' to_binomial_name(x)

to_binomial_name <- function(x) {

if (missing(x)) {
stop("Argument 'x' is required", call. = FALSE)
}

if (!is.character(x)) {
stop("Argument 'x' must be a character", call. = FALSE)
}


## Remove author and year ----

x <- gsub("\\(.+\\)", "", x)


## Clean string ----

x <- gsub("[[:punct:]]|[[:digit:]]", " ", x)
x <- gsub("\\s+", " ", x)
x <- trimws(x)


## Correct case ----

x <- tolower(x)
substr(x, 1, 1) <- toupper(substr(x, 1, 1))

x
}
21 changes: 21 additions & 0 deletions man/to_binomial_name.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 55c22e5

Please sign in to comment.