tolower, toupper, casefold & chartr R Functions (3 Examples)

 

This article explains how to convert characters from upper to lower case or vice versa in R.

In the tutorial, I will show examples for the R functions tolower(), toupper(), casefold(), and chartr().

Let’s dig in!

 

Example Data

Before we can move on to the examples, we need to create a character string in R. In this tutorial, we will use the character string “Example”:

x <- "Example"                            # Create example character string

 

Example 1: tolower & toupper R Functions

In the first example, I’ll explain how to use the tolower and toupper R functions.

We can convert all characters of our string to lower case with the tolower command:

tolower(x)                                # Convert to lower case letters
# "example"

The toupper command, in contrast, is used to convert all characters to upper case:

toupper(x)                                # Convert to upper case letters
# "EXAMPLE"

 

Example 2: casefold Function

The casefold R function also allows for the translation to small or capital letters.

Our characters are transformed to lower case if we specify upper = FALSE (default option) within the casefold function…

casefold(x, upper = FALSE)                # Convert to lower case letters
# "example"

…or to upper case if we specify upper = TRUE:

casefold(x, upper = TRUE)                 # Convert to upper case letters
# "EXAMPLE"

In practice, the casefold function is not different compared to tolower and toupper. However, some R users prefer to use it, since it provides a better compatibility with S-PLUS.

 

Example 3: chartr Function

The tolower, toupper, and casefold functions are used to convert an entire character string to lower or upper case. If we want to convert some characters to lower and others to upper case, we can apply the chartr function:

chartr(old = "Exam", new = "eXaM", x)     # Translate to upper and lower case
# "eXaMple"

The chartr function allows the specification of an old character pattern as well as of a new character pattern. The new pattern then replaces the old pattern.

 

Tutorial Video & Further Resources for Character Manipulation in R

Below, you can find a video on the Statistics Globe YouTube channel where I describe the steps of this tutorial in expanded detail:

 

 

This tutorial gave a quick overview on functions for the conversion to small and upper case letters. However, if you need further details on the functions, I can recommend to have a look at the R help documentation:

 

R Help chartr tolower toupper & casefold functions in R

Figure 1: R Help Documentation of tolower, toupper, casefold, and chartr Functions.

 

In addition, you could have a look at the following YouTube video of Tutorials Point. The video explains how to handle strings in R in general:

 

 

Furthermore, you could have a look at the other R programming tutorials on this website. I’m publishing new tutorials on the handling of character strings on a regular basis:

This R tutorial explained how to apply tolower, toupper, casefold, and chartr in R. However, if you have further questions do not hesitate to let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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.

Top