Convert All Character String Variables in Data Frame to Uppercase in R (Example)


In this tutorial, I’ll illustrate how to change lower case letters in character variables to uppercase in the R programming language.

The content of the post looks as follows:

So now the part you have been waiting for – the example:


Example Data

First, we’ll have to create some data that we can use in the following examples:

data <- data.frame(x1 = letters[1:5],    # Create data
                   x2 = 1:5,
                   x3 = c("hello", "hI", "StRiNg", "x", "xXx"),
                   stringsAsFactors = FALSE)
data                                     # Show data
#   x1 x2     x3
# 1  a  1  hello
# 2  b  2     hI
# 3  c  3 StRiNg
# 4  d  4      x
# 5  e  5    xXx

The previous output of the RStudio console shows that our example data consists of two character variables and one numeric variable.


Example: Converting Character Columns to Uppercase Using toupper Function

In this Example, I’ll illustrate how to adjust small letters to big letters with the toupper function in R. Consider the following R syntax:

data_new <- data.frame(lapply(data,      # Convert data with toupper function
                              function(variables) {
                                if (is.character(variables)) {
                                } else {
                       stringsAsFactors = FALSE)
data_new                                 # Show new data
#   x1 x2     x3
# 1  A  1  HELLO
# 2  B  2     HI
# 3  C  3 STRING
# 4  D  4      X
# 5  E  5    XXX

Admittedly, the previous R code is relatively complex. However, as you can see we converted all character strings in our data table to uppercase.


Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. I’m explaining how to capitalize characters based on the R syntax of this tutorial in the video.



In addition, you may want to read the other tutorials on this homepage. You can find some other articles about the manipulation of character strings below.


At this point you should know how to switch from lowercase to uppercase in R programming. In case you have any additional questions, please let me know in the comments section.


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.