r - Capitalize the first letter of both words in a two word string

ID : 20325

viewed : 13

Tags : rstringtitle-caser

Top 5 Answer for r - Capitalize the first letter of both words in a two word string

vote vote

100

There is a build-in base-R solution for title case as well:

tools::toTitleCase("demonstrating the title case") ## [1] "Demonstrating the Title Case" 

or

library(tools) toTitleCase("demonstrating the title case") ## [1] "Demonstrating the Title Case" 
vote vote

88

The base R function to perform capitalization is toupper(x). From the help file for ?toupper there is this function that does what you need:

simpleCap <- function(x) {   s <- strsplit(x, " ")[[1]]   paste(toupper(substring(s, 1,1)), substring(s, 2),       sep="", collapse=" ") }  name <- c("zip code", "state", "final count")  sapply(name, simpleCap)       zip code         state   final count     "Zip Code"       "State" "Final Count"  

Edit This works for any string, regardless of word count:

simpleCap("I like pizza a lot") [1] "I Like Pizza A Lot" 
vote vote

72

Match a regular expression that starts at the beginning ^ or after a space [[:space:]] and is followed by an alphabetical character [[:alpha:]]. Globally (the g in gsub) replace all such occurrences with the matched beginning or space and the upper-case version of the matched alphabetical character, \\1\\U\\2. This has to be done with perl-style regular expression matching.

gsub("(^|[[:space:]])([[:alpha:]])", "\\1\\U\\2", name, perl=TRUE) # [1] "Zip Code"    "State"       "Final Count" 

In a little more detail for the replacement argument to gsub(), \\1 says 'use the part of x matching the first sub-expression', i.e., the part of x matching (^|[[:spacde:]]). Likewise, \\2 says use the part of x matching the second sub-expression ([[:alpha:]]). The \\U is syntax enabled by using perl=TRUE, and means to make the next character Upper-case. So for "Zip code", \\1 is "Zip", \\2 is "code", \\U\\2 is "Code", and \\1\\U\\2 is "Zip Code".

The ?regexp page is helpful for understanding regular expressions, ?gsub for putting things together.

vote vote

66

Use this function from stringi package

stri_trans_totitle(c("zip code", "state", "final count")) ## [1] "Zip Code"      "State"       "Final Count"   stri_trans_totitle("i like pizza very much") ## [1] "I Like Pizza Very Much" 
vote vote

52

Alternative:

library(stringr) a = c("capitalise this", "and this") a [1] "capitalise this" "and this"        str_to_title(a) [1] "Capitalise This" "And This"    

Top 3 video Explaining r - Capitalize the first letter of both words in a two word string

Related QUESTION?