Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function turning a string into acronym in R

I have a string "California Art Craft Painting Society" and I want to write a function x in R that can turn any string into an acronym "CACPS". I used the following code:

acronym <- function(x){
  abbreviate(x)
}  

Is there a way to write the function using stringr such as strsplit the string first, then use strsub to pull the first letter of each word (not use abbreviate)?

like image 618
Jake Parker Avatar asked Nov 18 '25 02:11

Jake Parker


1 Answers

This might be of use. It uses str_remove_all on characters which are not preceded by a word break, and also removes any spaces.

library(stringr)

create_acronym <- function(x){
      str_remove_all(x , "(?<!\\b)\\w|\\s" ) 
    }
like image 152
Brendan Avatar answered Nov 19 '25 15:11

Brendan