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 (TRUEorFALSE). IfTRUE, the lowercase characters are removed first. If the value isFALSE, the lowercase characters will be removed after the operation.dot: This takes a logical value (TRUEorFALSE) whether a dot (.) is appended or not. The default value isTRUE.strict: This takes a logical value (TRUEorFALSE) whether or not theminlengthis 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 (TRUEorFALSE) whether thenameswith the original vector are returned. The default value isTRUE.
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 namesstates <- 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 abbreviationsstates_abbr
Explanation
- Line 2: We create a variable
statescontaining the words to be abbreviated. - Line 6: We implement the
abbreviate()function on thestatesvariable. The result is assigned to another variablestates_abbr. - Line 9: We print the variable containing the abbreviated words
states_abbr.