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?") my_text
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:
- The text to convert into capitals
- 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]) my_df
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) my_df
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.
library("dplyr") 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:
- mutate, to add or modify existing columns.
- across, to apply a certain action to multiple columns.
- where, to specify a condition to which columns the action is applied.
- is.character, to define the constraint of the condition.
- tolower, to convert all character columns into capitals.
In the R code below we combine these functions.
library("dplyr") 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.