Skip to content
Prev 245014 / 398502 Next

How to apitalize leading letters & else of personal names?

On Dec 14, 2010, at 9:00 PM, RockO wrote:

            
Here are four individually crafted gsub functions that could be  
serially applied:

 > gsub("^([a-z])", "\\U\\1", names, perl=TRUE)
[1] "Jean-francois st-john" "Helene o'donnel"       "Joe mcintyre"

 > gsub(" ([a-z])", " \\U\\1", names, perl=TRUE)
[1] "jean-francois St-john" "helene O'donnel"       "joe Mcintyre"

 > gsub("\\-([a-z])", "-\\U\\1", names, perl=TRUE)
[1] "jean-Francois st-John" "helene o'donnel"       "joe mcintyre"

 > gsub("\\'([a-z])", "'\\U\\1", names, perl=TRUE)
[1] "jean-francois st-john" "helene o'Donnel"       "joe mcintyre"


 > t2 <- gsub("^([a-z])", "\\U\\1", names, perl=TRUE)
 > t2 <- gsub(" ([a-z])", " \\U\\1", t2, perl=TRUE)
 > t2 <- gsub("\\-([a-z])", "-\\U\\1", t2, perl=TRUE)
 > t2 <-  gsub("\\'([a-z])", "'\\U\\1", t2, perl=TRUE)
 > t2
[1] "Jean-Francois St-John" "Helene O'Donnel"       "Joe Mcintyre"

Oooops forgot the mc:
 > gsub("Mc([a-z])", "Mc\\U\\1", t2, perl=TRUE)
[1] "Jean-Francois St-John" "Helene O'Donnel"       "Joe McIntyre"