I am trying to add an underscore before each uppercase letter followed by a lower case. Here is an example:
cases <- c("XrefAcctnoAcctID", "NewXref1AcctID", "NewXref2AcctID", "ClientNo")
I have it:
[1] "XrefAcctnoAcctID" "NewXref1AcctID"
[3] "NewXref2AcctID" "ClientNo"
And I want to have this:
"xref_acctno_acct_id"
"new_xref1_acct_id"
"new_xref2_acct_id"
"client_no"
I can go this far:
> tolower(gsub("([a-z])([A-Z])", "\\1_\\2", cases))
[1] "xref_acctno_acct_id" "new_xref1acct_id"
[3] "new_xref2acct_id" "client_no"
But it "new_xref1acct_id" "new_xref2acct_id"does not reflect what I want.
source
share