我正在编写一个自动执行变量名清除的函数,以生成同质的变量名。通常将类似“ This is anExample”的内容转换为“ This.Is.An.Example”。通常,所有这些操作都非常容易,但是对于包含在变量名中的首字母缩略词,我遇到了麻烦。
一个例子是:“时间PST”最好变成“ Clock.In.Time.PST”
我曾尝试将str_to_upper修改为一个函数,但是我对C的使用并不了解,而C似乎是编写stringi的背景。
我唯一的下一个想法是对字符串是否没有空格或标点进行条件检查,然后在前一个字母为小写的情况下在大写字母之前插入空格。那是我唯一真正的想法,那就是如何处理它。
Example<-c("Easy Example Test","Medium..example TEst","HaRd exampleTEST","Truly HARd TestCase PST")
#Step 1 - Removes all punctuation replacing with spaces
Example<-stringr::str_replace_all(Example,"[[:punct:]]"," ")
#Step 2 - This inserts a space wherever an uppercase letter is found with a preceding lowercase letter.
Example<-stringr::str_replace_all(Example,"([[:lower:]](?=[[:upper:]])|[[:upper:]](?=[[:upper:]][[:lower:]]))","\\1 ")
#Step 3 - This replaces all consecutive spaces with a period
Example<-stringr::str_replace_all(names(df),"\\s{1,}",".")
Current.Outcome<-c("Easy.Example.Test","Medium.example.T.Est","Ha.Rd.example.TEST","Truly.HA.Rd.Test.Case.PST")
Ideal.Outcome<-c("Easy.Example.Test","Medium.Example.Test","Hard.Example.Test","Truly.Hard.Test.Case.PST")