How to Convert Text into Uppercase in R [Examples]

In this article, we discuss how to convert all characters in a text string to uppercase in R.

In R, the best way to transform the characters of a text string to uppercase is by using the toupper() function. This function takes a vector or column of text as input and returns it in all capital characters.

Next, we will demonstrate some examples of how to use the toupper() function as well as a second, little-known function to convert text to uppercase in R.

How to Convert a Text String to Uppercase

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

my_text <- c("hello", "goodbye", "How Are You?")

Convert a Text String to Uppercase with the toupper() Function

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

The toupper() 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 uppercase.


Like the toupper() function, the R programming language also provides the tolower() function to convert all characters to lowercase.

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


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

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

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

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

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

casefold(<some_text>, upper = TRUE)

In contrast, if you use upper = FALSE, then all characters are changed to lowercase. 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 the text into uppercase.

casefold(my_text, upper = TRUE)

How to Convert a Text Column to Uppercase

Above we demonstrated how to convert text vectors into uppercase. 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 uppercase.

my_df <- data.frame(x1 = c("hi", "Hello", "howdy", "WeLcOmE"),
                    x2 = 1:4,
                    x3 = c("goodbye", "Bye Bye", "ciao", "adios"),
                    x4 = letters[23:26])

Convert a Text Column to Uppercase with the toupper() Function

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

As its argument, the toupper() function requires a text column and, as a result, returns the same column in its uppercase 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 all capitals.

my_df$x1 <- toupper(my_df$x1)

Convert a Text Column to Uppercase 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 uppercase with dplyr, you need the mutate() function and the toupper() function. The mutate() function lets you select the column you want to modify. Whereas, the toupper() function actually converts all characters into capitals.

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

my_df %>% 
 mutate(x1 = toupper(x1)) 

Convert All Text Columns to Uppercase with dplyr

In the examples above, we converted one column from a data frame into uppercase. If you have a 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 uppercase without specifying them one by one?

The easiest way to convert all columns from an R data frame into uppercase 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 capitals.

In the R code below we combine these functions.


my_df %>% 
  mutate(across(where(is.character), toupper))  

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