ATMathew
ATMathew

Reputation: 12866

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

Let's say that I have a two word string and I want to capitalize both of them.

name <- c("zip code", "state", "final count")

The Hmisc package has a function capitalize which capitalized the first word, but I'm not sure how to get the second word capitalized. The help page for capitalize doesn't suggest that it can perform that task.

library(Hmisc)
capitalize(name)
# [1] "Zip code"    "State"       "Final count"

I want to get:

c("Zip Code", "State", "Final Count")

What about three-word strings:

name2 <- c("I like pizza")

Upvotes: 203

Views: 137845

Answers (15)

Martin Morgan
Martin Morgan

Reputation: 46886

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 (^|[[:space:]]). 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.

Upvotes: 105

m_c
m_c

Reputation: 516

This might be of use to some. If the word is capitalized one first has to make it lowercase.

tools::toTitleCase("FRANCE")
[1] "FRANCE"

as opposed to

tools::toTitleCase(tolower("FRANCE"))
[1] "France"

Upvotes: 2

Salix
Salix

Reputation: 1383

✓ one line
✓ one existing function; no new package
✓ works on list/all words
✓ capitalizes the first letter AND lowers the rest of the word :

name <- c("zip CODE", "statE", "final couNt")
gsub("([\\w])([\\w]+)", "\\U\\1\\L\\2", name, perl = TRUE)
[1] "Zip Code"    "State"       "Final Count"

If you plan on using it a lot, I guess you could make a wrapper function with it :

capFirst <- function(x) gsub("([\\w])([\\w]+)", "\\U\\1\\L\\2", x, perl = TRUE)
capFirst(name)

If you have special letters you can use this reprex instead:

capFirst <- function(x) gsub("(\\p{L})(\\p{L}+)", "\\U\\1\\L\\2", x, perl = TRUE)
capFirst(name)

Except perl doesn't know how to make it upper- or lowercase after... So there's always:

stringi::stri_trans_totitle(c("zip CODE", "éTAts", "final couNt"))
#[1] "Zip Code"    "États"       "Final Count"

Upvotes: 2

Chriss Paul
Chriss Paul

Reputation: 1101

Another version using StrCap in DescTools

Text = c("This is my phrase in r", "No, this is not my phrase in r")

DescTools::StrCap(Text) # Default only first character capitalized
[1] "This is my phrase in r"         "No, this is not my phrase in r"

DescTools::StrCap(Text, method = "word") # Capitalize each word
[1] "This Is My Phrase In R"        "No This Is Not My Phrase In R"

> DescTools::StrCap(Text, method = "title") # Capitalize as in titles
[1] "This Is My Phrase in R"         "No, This Is Not My Phrase in R"

Upvotes: 2

David J. Bosak
David J. Bosak

Reputation: 1624

Here is a slight improvement on the accepted answer that avoids having to use sapply(). Also forces non-first characters to lower.

titleCase <- Vectorize(function(x) {
  
  # Split input vector value
  s <- strsplit(x, " ")[[1]]
  
  # Perform title casing and recombine
  ret <- paste(toupper(substring(s, 1,1)), tolower(substring(s, 2)),
        sep="", collapse=" ")
  
  return(ret)
  
}, USE.NAMES = FALSE)


name <- c("zip CODE", "statE", "final couNt")

titleCase(name)

#> "Zip Code"       "State" "Final Count" 

Upvotes: 2

Taz
Taz

Reputation: 556

You could also use the snakecase package:

install.packages("snakecase")
library(snakecase)

name <- c("zip code", "state", "final count")
to_title_case(name)
#> [1] "Zip Code"    "State"       "Final Count"

# or 
to_upper_camel_case(name, sep_out = " ")
#> [1] "Zip Code"    "State"       "Final Count"

https://github.com/Tazinho/snakecase

Upvotes: 5

Cole Davis
Cole Davis

Reputation: 21

This gives capital Letters to all major words

library(lettercase)
xString = str_title_case(xString)

Upvotes: 2

petermeissner
petermeissner

Reputation: 12900

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"

Upvotes: 207

Brijesh
Brijesh

Reputation: 796

Alternative:

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

Upvotes: 60

Dirk
Dirk

Reputation: 1324

The package BBmisc now contains the function capitalizeStrings.

library("BBmisc")
capitalizeStrings(c("the taIl", "wags The dOg", "That Looks fuNny!")
    , all.words = TRUE, lower.back = TRUE)
[1] "The Tail"          "Wags The Dog"      "That Looks Funny!"

Upvotes: 9

greg L
greg L

Reputation: 4144

Alternative way with substring and regexpr:

substring(name, 1) <- toupper(substring(name, 1, 1))
pos <- regexpr(" ", name, perl=TRUE) + 1
substring(name, pos) <- toupper(substring(name, pos, pos))

Upvotes: 6

bartektartanus
bartektartanus

Reputation: 16080

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"

Upvotes: 88

Andrie
Andrie

Reputation: 179578

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"

Upvotes: 188

diliop
diliop

Reputation: 9451

Try:

require(Hmisc)
sapply(name, function(x) {
  paste(sapply(strsplit(x, ' '), capitalize), collapse=' ')
})

Upvotes: 21

Chase
Chase

Reputation: 69251

From the help page for ?toupper:

.simpleCap <- function(x) {
    s <- strsplit(x, " ")[[1]]
    paste(toupper(substring(s, 1,1)), substring(s, 2),
          sep="", collapse=" ")
}


> sapply(name, .simpleCap)

zip code         state   final count 
"Zip Code"       "State" "Final Count"

Upvotes: 16

Related Questions