Search code examples
rgsubstringrregexp-replace

R capitalize roman numerals only in string


If I have a vector with the following:

people <- c("PERSON I", "PERSON II", "PERSON III", "PERSON IV")

To turn them into title case, I used the following:

people <- str_to_title(people)

Now I have the following

> people
[1] "Person I"   "Person Ii"  "Person Iii" "Person Iv"  

What do I to capitalize the Roman numerals only like this?

"Person I"   "Person II"   "Person III"   "Person IV" 

Or is there a way to convert the all-caps into the last vector without using str_to_title?


Solution

  • Here is a base R option using substr, sub, and paste:

    people <- c("PERSON I", "PERSON II", "PERSON III", "PERSON IV")
    people <- paste0(substr(people, 1, 1), tolower(sub("^\\S(\\S+).*$", "\\1", people)),
                     " ", sub("^.*?(\\S+)$", "\\1", people))
    people
    
    [1] "Person I"   "Person II"  "Person III" "Person IV"