Thursday, September 14, 2017

abbreviate() {base}


abbreviate() function abbreviates strings to a specific minimum length.

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

The parameters are:
 -names.arg:names to be abbreviated
 -minlegth: minimum length of the abbreviation
 -use.classes: logical parameter. Lower cases are removed first
 -dot: logical parameter. Adds a dot at the end of the abbreviations
 -strict: logical parameter. The words are abbreviated to the minlenght regardless if they are unique or not.
 -method: method to be used (“left.kept” or “both.sides”)
 -named: logical parameter. Show original names.

minlength and strict parameters:
x =c("abcde", "abcdefgh", "abce", "egafg", "ghty")
x
## [1] "abcde"    "abcdefgh" "abce"     "egafg"    "ghty"
#minlength
abbreviate(x, minlength = 2) #the function abbreviates the words remaining they unique, for that reason some have a length bigger than 2
##    abcde abcdefgh     abce    egafg     ghty 
##  "abcde"  "abcdf"   "abce"     "eg"     "gh"
#minlenght = 5
abbreviate(x, 5) 
##    abcde abcdefgh     abce    egafg     ghty 
##  "abcde"  "abcdf"   "abce"  "egafg"   "ghty"
#strict = TRUE
abbreviate(x, 2, strict = TRUE) #when using strict = TRUE the words are abbreviated to the minlenght regardless if they are unique or not
##    abcde abcdefgh     abce    egafg     ghty 
##     "ab"     "ab"     "ab"     "eg"     "gh"

use.classes parameter:
y =c("ABCDE", "ABCdefgh", "abcET", "EgafgG", "dghTY", "abcDEhhhht", "afth")
y
## [1] "ABCDE"      "ABCdefgh"   "abcET"      "EgafgG"     "dghTY"     
## [6] "abcDEhhhht" "afth"
#use.classes = TRUE
abbreviate(y, 1, use.classes = TRUE) 
##      ABCDE   ABCdefgh      abcET     EgafgG      dghTY abcDEhhhht 
##     "ABCD"     "ABCd"       "aE"        "E"        "d"       "aD" 
##       afth 
##       "af"
abbreviate(y, 2, use.classes = TRUE) #the first letter is kept regardless if it is an upper or lower case, then the upper cases have preference.
##      ABCDE   ABCdefgh      abcET     EgafgG      dghTY abcDEhhhht 
##     "ABCD"     "ABCd"       "aE"       "EG"       "dT"       "aD" 
##       afth 
##       "af"
abbreviate(y, 4, use.classes = TRUE) 
##      ABCDE   ABCdefgh      abcET     EgafgG      dghTY abcDEhhhht 
##     "ABCD"     "ABCd"     "abET"     "EgfG"     "dgTY"     "abDE" 
##       afth 
##     "afth"

dot parameter:
y =c("ABCDE", "ABCdefgh", "abcET", "EgafgG", "dghTY", "abcDEhhhht", "afth")
y
## [1] "ABCDE"      "ABCdefgh"   "abcET"      "EgafgG"     "dghTY"     
## [6] "abcDEhhhht" "afth"
abbreviate(y, 2, use.classes = TRUE, dot = TRUE)
##      ABCDE   ABCdefgh      abcET     EgafgG      dghTY abcDEhhhht 
##    "ABCD."    "ABCd."      "aE."      "EG."      "dT."      "aD." 
##       afth 
##      "af."
abbreviate(y, 2, strict = TRUE, use.classes = TRUE, dot = TRUE)
##      ABCDE   ABCdefgh      abcET     EgafgG      dghTY abcDEhhhht 
##      "AB."      "AB."      "aE."      "EG."      "dT."      "aD." 
##       afth 
##      "af."

method parameter: 
method has two options: left.kept(default) and both.sides
z =c("abcde", "abcdeft", "acdef", "qwert", "werty")
z
## [1] "abcde"   "abcdeft" "acdef"   "qwert"   "werty"
#method = "left.kept"
abbreviate(z, 2, method = "left.kept")
##   abcde abcdeft   acdef   qwert   werty 
## "abcde" "abcdf"    "ac"    "qw"    "wr"
#method = "both.sides"
abbreviate(z, 2, method = "both.sides")
##   abcde abcdeft   acdef   qwert   werty 
##    "ab"    "ft"    "ac"    "qw"    "wr"

named parameter:
#named = TRUE
abbreviate(z, 2, named = TRUE)
##   abcde abcdeft   acdef   qwert   werty 
## "abcde" "abcdf"    "ac"    "qw"    "wr"
#names = FALSE
abbreviate(z, 2, named = FALSE)
## [1] "abcde" "abcdf" "ac"    "qw"    "wr"

Working with the iris data set:
abbreviate(colnames(iris), 2)
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##        "S.L"        "S.W"        "P.L"        "P.W"         "Sp"
abbreviate(colnames(iris), 2, strict = TRUE)
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##         "S."         "S."         "P."         "P."         "Sp"
abbreviate(colnames(iris), 2, strict = TRUE, dot = TRUE)
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##        "S.."        "S.."        "P.."        "P.."        "Sp."
abbreviate(colnames(iris), 4, strict = TRUE, named = FALSE)
## [1] "Sp.L" "Sp.W" "Pt.L" "Pt.W" "Spcs"
par(mfrow = c(1,2))
boxplot(iris, col = c('deeppink', 'deeppink1', 'deeppink2','deeppink3', 'deeppink4'), las = 2)
boxplot(iris, names = c(abbreviate(colnames(iris), 2, named = FALSE)), col = c('coral', 'coral1', 'coral2','coral3', 'coral4'), las = 2)

No comments:

Post a Comment

duplicated() {base}

duplicated()  function determines which elements are duplicated and returns a logical vector. The parameters of the function are:   ...