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



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

let's have 2 word string , want capitalize both of them.

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

the hmisc bundle has function capitalize capitalized first word, i'm not sure how sec word capitalized. help page capitalize doesn't suggest can perform task.

> library(hmisc) > capitalize(name) [1] "zip code" "state" "final count"

i want get:

"zip code" "state" "final count"

what 3 word strings:

name2 <- c("i pizza")

the base of operations r function perform capitalization toupper(x). help file ?toupper there function 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 works string, regardless of word count:

simplecap("i pizza lot") [1] "i pizza lot"

r string title-case

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -