R Capitalize First Letter of Each Word in Character String (3 Examples)

 

In this post, I’ll explain how to capitalize the first letter of each word of a character string in the R programming language.

The tutorial consists of these contents:

Let’s dive into it.

 

Creation of Exemplifying Data

In the examples of this R tutorial, we’ll use the following character string as example data:

my_string <- "hello this is my string"               # Create example string

As you can see, our example string simply contains the four words “hello this is my string”. All of the characters are in lower case.

In the following examples, I’ll show you three alternatives on how to capitalize the first letters of these words…

 

Example 1: Capitalize First Letter with Base R

Example 1 shows the capitalization with the base installation of the R programming language (i.e. without using any additional packages).

Have a look at the following R code and the output below that is printed to the RStudio console:

gsub("(^|[[:space:]])([[:alpha:]])", "\\1\\U\\2",    # Uppercase with Base R
     my_string,
     perl = TRUE)
# "Hello This Is My String"

As you can see, we converted all first letters to upper case. You can simply copy/paste this code to your own R program and replace the name of your string (or vector/column of strings).

Admittedly, this R syntax is a bit complicated. Fortunately, R provides numerous add-on packages that contain simplified solutions for capitalizing character strings in R. Keep on reading!

 

Example 2: Capitalize First Letter with stringi Package

In this example, I’ll show you how to capitalize the first letter of each word based on the stringi R package. First, we have to install and load the package to R:

install.packages("stringi")                          # Install stringi package
library("stringi")                                   # Load stringi package

Now, we can apply the stri_trans_totitle function as follows:

stri_trans_totitle(my_string)                        # Apply stri_trans_totitle function
# "Hello This Is My String"

Same output as in Example 1, but the R code is much simpler.

 

Example 3: Capitalize First Letter with tools Package

In the third example, I’ll show you how to capitalize all first letters, besides certain fill-words such as articles. We can do that based on the tools package:

install.packages("tools")                          # Install tools package
library("tools")                                   # Load tools package

The tools package contains the R command toTitleCase. We can apply this command as follows:

toTitleCase(my_string)                             # Apply toTitleCase function
# "Hello this is My String"

As you can see based on the output of the RStudio console, the toTitleCase function converted some but not all first letters to uppercase. The words “this” and “is” where kept in lower case.

Whether you prefer to capitalize all or just some letters is a matter of style. However, now you know all the available options!

 

Video, Further Resources & Summary

If you need more explanations on the contents of this article, you may watch the following video of the Statistics Globe YouTube channel. I’m explaining the R code of this tutorial in the video instruction:

 

The YouTube video will be added soon.

 

Additionally, you could have a look at the other posts on my website:

 

This article showed how to change the case of each first letter to uppercase in R programming. In case you have any additional questions, please let me know in the comments section.

 



Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top