-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
|
||
|