Capitalizing letters. R equivalent of excel "PROPER" function


This question already has an answer here:


The question is about an equivalent of Excel PROPER and the (former) accepted answer is based on:

proper=function(x) paste0(toupper(substr(x, 1, 1)), tolower(substring(x, 2)))

It might be worth noting that:

proper("hello world")
## [1] "Hello world"

Excel PROPER would give, instead, "Hello World". For 1:1 mapping with Excel see @Matthew Plourde.

If what you actually need is to set only the first character of a string to upper-case, you might also consider the shorter and slightly faster version:

proper=function(s) sub("(.)", ("\\U\\1"), tolower(s), pe=TRUE)

With regular expressions:

x <- c('woRd Word', 'Word', 'word words')
gsub("(?<=\\b)([a-z])", "\\U\\1", tolower(x), perl=TRUE)
# [1] "Word Word"  "Word"       "Word Words"

(?<=\\b)([a-z]) says look for a lowercase letter preceded by a word boundary (e.g., a space or beginning of a line). (?<=...) is called a "look-behind" assertion. \\U\\1 says replace that character with it's uppercase version. \\1 is a back reference to the first group surrounded by () in the pattern. See ?regex for more details.

If you only want to capitalize the first letter of the first word, use the pattern "^([a-z]) instead.

Another method uses the stringi package. The stri_trans_general function appears to lower case all letters other than the initial letter.

x <- c('woRd Word', 'Word', 'word words')
stri_trans_general(x, id = "Title")
[1] "Word Word"  "Word"       "Word Words"

I dont think there is one, but you can easily write it yourself

(dat <- data.frame(x = c('hello', 'frIENds'),
                   y = c('rawr','rulZ'),
                   z = c(16, 18)))
#         x    y  z
# 1   hello rawr 16
# 2 frIENds rulZ 18

proper <- function(x)
  paste0(toupper(substr(x, 1, 1)), tolower(substring(x, 2)))

(dat <- data.frame(lapply(dat, function(x)
  if (is.numeric(x)) x else proper(x)),
  stringsAsFactors = FALSE))

#         x    y  z
# 1   Hello Rawr 16
# 2 Friends Rulz 18

# 'data.frame':  2 obs. of  3 variables:
#   $ x: chr  "Hello" "Friends"
#   $ y: chr  "Rawr" "Rulz"
#   $ z: num  16 18

Need Your Help

making matplotlib scatter plots from dataframes in Python's pandas

python matplotlib plot dataframe pandas

What is the best way to make a series of scatter plots using matplotlib from a pandas dataframe in Python?