How to Convert Text into Lowercase in R [Examples]

In this article, we discuss how to convert text to lowercase in R. We demonstrate how to change the case of a simple string, a column, or all columns from a data frame to lowercase.

In R, the best way to convert a text string to all lowercase characters is by using the tolower() function. This function takes a character vector or character column as input and returns the text in its transformed lowercase version.

Here we demonstrate some examples of how to use the tolower() function as well as a second, little-known function to convert text to lowercase in R.

How to Convert a Text String to Lowercase

In the examples below, we use a character vector with 3 elements. These elements are completely or partially uppercase and we want to convert them into lowercase.

my_text <- c("HELLO", "GOODBYE", "How Are You?")
my_text
An R vector with strings

Convert a Text String to Lowercase with the tolower() Function

The most convenient way to transform text to lowercase in R is with the tolower() function.

The tolower() function is an R base function and therefore doesn’t require loading additional packages. Its syntax is simple because it requires just one argument, namely the text to be transformed to lowercase.

tolower(<some_text>)

In the example below, we use the tolower() function to convert the original text strings into their lowercase version.

tolower(my_text)
Convert a Text String in R to Lowercase with the tolower() function

Like to tolower() function, R also provides the toupper() function to convert all characters into uppercase.

Convert a Text String to Lowercase with the casefold() Function

A second, less-known function to convert text into lowercase in R is the casefold() function.

The casefold() function is also an R base function to change the case of some text. Unlike the tolower() function, the casefold() function requires two arguments, namely:

  1. The text to convert into lowercase
  2. The upper=-argument.

By setting the upper=-argument to FALSE, the casefold() function transforms the text into lowercase.

casefold(<some_text>, upper = FALSE)

In contrast, if you use upper = TRUE, then all characters are changed to uppercase. Therefore, you can use this function for two purposes.

The R code below contains an example of how to use the casefold() function to transform text into lowercase.

casefold(my_text, upper = FALSE)
Convert a Text String in R to Lowercase with the casefold() function

How to Convert a Text Column to Lowercase

Above we demonstrated how to convert text vectors into lowercase. Here we will show how to do the same, but for text columns.

We will use the following data frame to support the examples. This data frame has 4 columns of which 3 contain character data. The goal is to convert either one column or all multiple into lowercase.

my_df <- data.frame(x1 = c("HI", "Hello", "HOWDY", "WeLcOmE"),
                    x2 = 1:4,
                    x3 = c("GOODBYE", "Bye Bye", "CIAO", "ADIOS"),
                    x4 = LETTERS[23:26])
my_df
An R dataframe

Convert a Text Column to Lowercase with the tolower() Function

Like converting the elements of a vector into lowercase, the easiest way to convert columns in R into lowercase is by using the tolower() function.

As its argument, the tolower() function requires a column and, as a result, returns the same column in its lowercase version. However, by just doing this the original column is not overwritten. Therefore you need the <- symbol.

See the example below where we convert the column x1 into lowercase.

my_df$x1 <- tolower(my_df$x1)
my_df
Convert a Columns of a Dataframe to Lowercase with the tolower() function

Convert a Text Column to Lowercase with dplyr

Many of us work with the tools the dplyr package provides to process and clean our data frames.

To convert text columns into lowercase with dplyr, you need the mutate() function and the tolower() function. The mutate() function lets you select the column you want to modify. Whereas, the tolower() function actually converts all characters to lowercase.

The R code below provides an example. Remember to (install and) load the dplyr package first.

library("dplyr")

my_df %>% 
 mutate(x1 = tolower(x1)) 
Convert a Text column in R to lowercase with the dplyr

Convert All Text Columns to Lowercase with dplyr

In the examples above, we converted one column from a data frame into lowercase. If you have few columns, you can repeat this process. However, it can be a tedious task if your data frame has many columns.

So, how do you transform all character columns into lowercase without specifying them one by one?

The easiest way to convert all columns from an R data frame into lowercase is by using a combination of 5 functions, namely:

  1. mutate, to add or modify existing columns.
  2. across, to apply a certain action to multiple columns.
  3. where, to specify a condition to which columns the action is applied.
  4. is.character, to define the constraint of the condition.
  5. tolower, to convert all character columns into lowercase.

In the R code below we combine these functions.

library("dplyr")

my_df %>% 
  mutate(across(where(is.character), tolower))   
Convert all Text columns in R to lowercase with the dplyr

As the image shows, all character columns where converted easily into lowercase without explicitly specifying them one by one.