string with a capital letter in R

Start every string with a capital letter in R

If you want to start every string with a capital letter in R, you should know a reliable function that works in every situation. Same as the PROPER function in Excel. Of course, it is possible to write your R functions, but sometimes it is not worth the effort.

Here is my text that I’m using to cover most of the possible situations.

JOHN doe, jEAN Paul, janis,sturis, Julia meryl-bush

The best results to transform every string with the capital letter in R is with the str_to_title function from the stringr package.

stringr::str_to_title("JOHN doe, jEAN Paul, janis,sturis, Julia meryl-bush")
#[1] "John Doe, Jean Paul, Janis,Sturis, Julia Meryl-Bush"

Results are as good as with the PROPER function in Microsoft Excel.

Sometimes the popular answer is toTitleCase from the tools package, but it works not so well in the most difficult situations.

tools::toTitleCase("JOHN doe, jEAN Paul, janis,sturis, Julia meryl-bush")

#[1] "JOHN Doe, jEAN Paul, Janis,sturis, Julia Meryl-Bush"

If you are interested to learn more about how to learn Excel as an R user, take a look at this Datacornering post.





Posted

in

,

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *