From f7f6c1aafe347bdb35b0af33bea5a3b25c37da0b Mon Sep 17 00:00:00 2001 From: crcruzr Date: Tue, 25 Jun 2024 17:35:42 -0400 Subject: [PATCH] minor changes --- internals.R | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 internals.R diff --git a/internals.R b/internals.R new file mode 100644 index 0000000..89c5ac4 --- /dev/null +++ b/internals.R @@ -0,0 +1,51 @@ +# Function to clean species list names +#' @keywords internal +standardize_names <- function(splist) { + fixed1 <- simple_cap(trimws(splist)) # all up + fixed2 <- gsub("cf\\.", "", fixed1) + fixed3 <- gsub("aff\\.", "", fixed2) + fixed4 <- trimws(fixed3) # remove trailing and leading space + fixed5 <- gsub("_", " ", fixed4) # change names separated by _ to space + + # Hybrids + fixed6 <- gsub("(^x )|( x$)|( x )", " ", fixed5) + hybrids <- fixed5 == fixed6 + if (!all(hybrids)) { + sp_hybrids <- splist[!hybrids] + warning(paste("The 'x' sign indicating hybrids have been removed in the", + "following names before search:", + paste(paste0("'", sp_hybrids, "'"), collapse = ", ")), + immediate. = TRUE, call. = FALSE) + } + # Merge multiple spaces + fixed7 <- gsub("(?<=[\\s])\\s*|^\\s+|\\s+$", "", fixed6, perl = TRUE) + return(fixed7) +} + +#' @keywords internal +simple_cap <- function(x) { + # Split each string into words, remove unnecessary white spaces, and convert to lowercase + words <- sapply(strsplit(x, "\\s+"), function(words) paste(tolower(words), collapse = " ")) + + # Capitalize the first letter of each word + capitalized <- sapply(strsplit(words, ""), function(word) { + if (length(word) > 0) { + word[1] <- toupper(word[1]) + } + paste(word, collapse = "") + }) + + return(capitalized) +} + +#' @keywords internal +#' +find_duplicates <- function(vector) { + # Count the frequency of each word + word_counts <- table(vector) + # Find words with a frequency greater than 1 + duplicated_words <- names(word_counts[word_counts > 1]) + return(duplicated_words) +} + +