Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalizing letters. R equivalent of excel "PROPER" function [duplicate]

Colleagues,

I'm looking at a data frame resembling the extract below:

Month   Provider Items
January CofCom   25
july    CofCom   331
march   vobix    12
May     vobix    0

I would like to capitalise first letter of each word and lower the remaining letters for each word. This would result in the data frame resembling the one below:

Month   Provider Items
January Cofcom   25
July    Cofcom   331
March   Vobix    12
May     Vobix    0

In a word, I'm looking for R's equivalent of the ROPER function available in the MS Excel.

like image 909
Konrad Avatar asked Jul 25 '14 13:07

Konrad


People also ask

How do you capitalize letters in R?

toupper() function in R Language is used to convert the lowercase letters to uppercase.

How do you capitalize the first letter of each word in R?

Convert First letter of every word to Uppercase in R Programming – str_to_title() Function. str_to_title() Function in R Language is used to convert the first letter of every word of a string to Uppercase and the rest of the letters are converted to lower case.

How do you capitalize words in Excel?

In cell B2, type =PROPER(A2), then press Enter. This formula converts the name in cell A2 from uppercase to proper case. To convert the text to lowercase, type =LOWER(A2) instead. Use =UPPER(A2) in cases where you need to convert text to uppercase, replacing A2 with the appropriate cell reference.

How do you capitalize the first letter in Excel VBA?

Let me explain how this formula works: LOWER(A2) – This converts the entire text into lower case. UPPER(LEFT(A2,1) – This converts the first letter of the text string in the cell into the upper case. REPLACE function is used to only replace the first character with the upper case version of it.


Video Answer


3 Answers

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)
like image 178
antonio Avatar answered Oct 16 '22 14:10

antonio


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

require(stringi)
x <- c('woRd Word', 'Word', 'word words')
stri_trans_general(x, id = "Title")
[1] "Word Word"  "Word"       "Word Words"
like image 35
lawyeR Avatar answered Sep 19 '22 16:09

lawyeR


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.

like image 29
Matthew Plourde Avatar answered Oct 16 '22 14:10

Matthew Plourde