Generate abbreviated strings, allowing for both a customized abbreviation (e.g., specific to country names) and a generic abbreviation.

create_abbreviated_label(x,
  abbrv.generic = function(x) abbreviate(gsub("[^[:alnum:] ]","",x)),
  abbrv.custom = NULL)

Arguments

x

A factor or character vector to be abbreviated.

abbrv.generic

Function accepting a single argument that returns a character vector containing abbreviations for the character strings in its first (and only) argument. The default calls the abbreviate function after first removing all non-alphanumeric characters.

abbrv.custom

Takes the same form as the abbrv.genetic function, but can also return NA for any character strings that are unrecognized.

Value

create_abbreviated_label returns either a character vector with abbreviated strings, or a factor in which the levels are abbreviated.

Details

create_abbreviated_label first applies the custom abbreviation, then applies the generic abbreviation to any entries that are unrecognized by the custom abbreviation (as indicated by NA).

See also

Examples

# Define custom abbreviations for some Canadian provinces and # territories. canada.post.abbrv <- function (x) unlist(list(Quebec = "QC",Ontario = "ON",Manitoba = "MB", Alberta = "AB",Saskatchewan = "SK",Yukon = "YT"))[x] # Function create_abbreviated_label can be applied to a factor or a # character vector. x <- c("Quebec","Alberta","Ontario","Alberta","Yukon") create_abbreviated_label(x,abbrv.custom = canada.post.abbrv)
#> Quebec Alberta Ontario Alberta Yukon #> "QC" "AB" "ON" "AB" "YT"
create_abbreviated_label(factor(x),abbrv.custom = canada.post.abbrv)
#> [1] QC AB ON AB YT #> Levels: AB ON QC YT