What is the abbreviate() function in R?

Overview

The abbreviate function in R abbreviates string characters to at least minlength characters. It does so in such a way that they remain unique(if they were) unless the minlength value should be observed strictly.

Syntax

abbreviate(names.arg, minlength = 4, use.classes = TRUE,
           dot = FALSE, strict = FALSE,
           method = c("left.kept", "both.sides"), named = TRUE)

Required parameter value

The abbreviate function takes the character vector, names.arg, which takes the vector of names to be abbreviated.

Optional parameter value

The abbreviate function takes the following optional parameter values:

  • minlength: This represents the minimum length of the abbreviations.
  • use.classes: This takes a logical value (TRUE or FALSE). If TRUE, the lowercase characters are removed first. If the value is FALSE, the lowercase characters will be removed after the operation.
  • dot: This takes a logical value (TRUE or FALSE) whether a dot (.) is appended or not. The default value is TRUE.
  • strict: This takes a logical value (TRUE or FALSE) whether or not the minlength is observed strictly.
  • method: This is a character string that specifies the method used (either to abbreviate from left or right). By default, it uses the "left.kept" method, which simply means abbreviating from the left side of the string.
  • named: This takes a logical value (TRUE or FALSE) whether the names with the original vector are returned. The default value is TRUE.

Return value

The abbreviate function returns a character vector containing abbreviations for the character strings in its first argument.

Example

# creating the character vector of names
states <- c ("Alabama", "Alaska", "Arizona", "Arkansas", "California")
# calling the abbreviation() function
# minlength of 4, dot = "TRUE", strict = "TRUE"
states_abbr = abbreviate(states,4, dot = "TRUE", strict = "TRUE")
# printing the abbreviations
states_abbr

Explanation

  • Line 2: We create a variable states containing the words to be abbreviated.
  • Line 6: We implement the abbreviate() function on the states variable. The result is assigned to another variable states_abbr.
  • Line 9: We print the variable containing the abbreviated words states_abbr.

Free Resources