This function takes a character vector of IDs and removes a specified suffix pattern. It also checks if removing the suffix results in new duplicate IDs that were not duplicates before the suffix removal.
Arguments
- ids
A character vector of identifiers.
- suffix_pattern
A string representing the regular expression pattern for the suffix to remove. Defaults to
"\\.\\d+$"(a hyphen followed by one or more digits at the end of the string). IfNULLorFALSEor an empty string, the original IDs are returned unmodified.
Value
A character vector of IDs with the specified suffixes removed. Issues a warning if new duplicates are created from previously unique suffixed IDs.
Examples
ids_to_clean <- c("ID-1", "ID-2.1", "ID-2.2", "ID-3", "ID-4.01", "ID-4.001")
remove_id_version_suffix(ids_to_clean, suffix_pattern = "\\.\\d+$")
#> Error in remove_id_version_suffix(ids_to_clean, suffix_pattern = "\\.\\d+$"): Removing ID version suffixes created new duplicate IDs.
# c("ID-1", "ID-2", "ID-2", "ID-3", "ID-4", "ID-4")
# Warning: Removing ID version suffixes created new duplicate IDs...
remove_id_version_suffix(ids_to_clean, suffix_pattern = "-\\d+$")
#> Error in remove_id_version_suffix(ids_to_clean, suffix_pattern = "-\\d+$"): Removing ID version suffixes created new duplicate IDs.
# c("ID", "ID-2.1", "ID-2.2", "ID", "ID-4.01", "ID-4.001")
# Warning: Removing ID version suffixes created new duplicate IDs...
remove_id_version_suffix(c("TX.1", "TX.2", "TX.3"), suffix_pattern = NULL)
#> [1] "TX.1" "TX.2" "TX.3"
# c("TX.1", "TX.2", "TX.3")