5 mapchr()

mapchr() is a general functional for altering character vectors: it applies any function to each of its elements. Similar to map_chr() from the purrr library with the exception that mapchr() only accepts character vectors as the data input. This function is useful for when manipulating the arrangement of the characters is desired.

The required inputs are f and x, respectively the function and character vector. The output is typically a character vector (depending on the function being passed).

5.0.1 Collapsing

rn_mc <- rownames(mtcars)
mapchr(function(x) paste0(x, collapse = '|'), rn_mc)
##  [1] "M|a|z|d|a| |R|X|4"                    
##  [2] "M|a|z|d|a| |R|X|4| |W|a|g"            
##  [3] "D|a|t|s|u|n| |7|1|0"                  
##  [4] "H|o|r|n|e|t| |4| |D|r|i|v|e"          
##  [5] "H|o|r|n|e|t| |S|p|o|r|t|a|b|o|u|t"    
##  [6] "V|a|l|i|a|n|t"                        
##  [7] "D|u|s|t|e|r| |3|6|0"                  
##  [8] "M|e|r|c| |2|4|0|D"                    
##  [9] "M|e|r|c| |2|3|0"                      
## [10] "M|e|r|c| |2|8|0"                      
## [11] "M|e|r|c| |2|8|0|C"                    
## [12] "M|e|r|c| |4|5|0|S|E"                  
## [13] "M|e|r|c| |4|5|0|S|L"                  
## [14] "M|e|r|c| |4|5|0|S|L|C"                
## [15] "C|a|d|i|l|l|a|c| |F|l|e|e|t|w|o|o|d"  
## [16] "L|i|n|c|o|l|n| |C|o|n|t|i|n|e|n|t|a|l"
## [17] "C|h|r|y|s|l|e|r| |I|m|p|e|r|i|a|l"    
## [18] "F|i|a|t| |1|2|8"                      
## [19] "H|o|n|d|a| |C|i|v|i|c"                
## [20] "T|o|y|o|t|a| |C|o|r|o|l|l|a"          
## [21] "T|o|y|o|t|a| |C|o|r|o|n|a"            
## [22] "D|o|d|g|e| |C|h|a|l|l|e|n|g|e|r"      
## [23] "A|M|C| |J|a|v|e|l|i|n"                
## [24] "C|a|m|a|r|o| |Z|2|8"                  
## [25] "P|o|n|t|i|a|c| |F|i|r|e|b|i|r|d"      
## [26] "F|i|a|t| |X|1|-|9"                    
## [27] "P|o|r|s|c|h|e| |9|1|4|-|2"            
## [28] "L|o|t|u|s| |E|u|r|o|p|a"              
## [29] "F|o|r|d| |P|a|n|t|e|r|a| |L"          
## [30] "F|e|r|r|a|r|i| |D|i|n|o"              
## [31] "M|a|s|e|r|a|t|i| |B|o|r|a"            
## [32] "V|o|l|v|o| |1|4|2|E"

5.1 jumble()

The function jumble(x) randomly changes the order of the characters in every element. The input and output is a character vector. This function can be useful for generating variations of existing passwords or other types of string vectors.

5.1.1 Jumbling characters

set.seed(0)
rn_mc <- rownames(mtcars)
jumble(rn_mc)
##  [1] "4dRMa zXa"           "aWzMa4g Ra dX"       "u1Dn0 ast7"         
##  [4] "iHnrtD e4 ovre"      "Stp ttobrouraHeon"   "nVlaati"            
##  [7] "r3 sDt0eu6"          "42c 0DeMr"           "cM 2re30"           
## [10] "80re 2cM"            "8cr e2MC0"           "E0e c54SrM"         
## [13] "rLSM c450e"          "Mr4L5S0 Cce"         "doaalwi cdlteolCFe" 
## [16] "CiitnLnnetoolnac nl" "irs leelyrphCraIm"   "1Ft 2ai8"           
## [19] "aidHic vCno"         "raoCtlaool oyT"      "rCtono oaToya"      
## [22] "alrg egdenhDeClo"    "Jv alnMiAeC"         "8a2C aromZ"         
## [25] "F iePobrirnacitd"    " 9ait1-FX"           "9-ec1r Pho24s"      
## [28] "oEuLtr saopu"        "rePaF ratL ndo"      "ain erFriorD"       
## [31] " orisaBMetaar"       "4o 2ovlV1E"