When working with strings in R, it can be handy to know how to quickly change its case. In this tutorial, we will look at how to convert the case of a string to uppercase in R with the help of some examples.
How do I convert a string to uppercase in R?
You can use the built-in toupper()
function to convert a string to uppercase in R. Pass the string you want to convert as an argument to the toupper()
function.
The following is the syntax –
# string s to uppercase toupper(s)
It returns the passed string as an uppercase string.
You can also apply this function to a vector of strings to convert each string in the vector to uppercase.
Let’s now look at some examples of using the toupper()
function.
Convert string to uppercase
Here, we will create a string with characters having mixed cases. That is some characters will be uppercase and some lowercase. We’ll then pass the string to the toupper()
function.
# create a string s <- "pLeAsE dO nOt tYPe lIkE THiS" # convert string to uppercase print(toupper(s))
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
[1] "PLEASE DO NOT TYPE LIKE THIS"
You can see that all the characters in the returned string are in uppercase.
Convert a vector of strings to uppercase
You can also apply the toupper()
function to a vector of strings.
Let’s look at an example.
Here, we will create a vector of some string values and then apply the toupper()
function on the vector itself.
# create a vector of strings vec <- c("HELLO", "Howdy", "hoLA", "namaste") # convert vector to uppercase print(toupper(vec))
Output:
[1] "HELLO" "HOWDY" "HOLA" "NAMASTE"
You can see that each string in the resulting vector is in uppercase.
You might also be interested in –
- R – Remove Whitespaces From String
- Convert Numeric Type to Character Type in R
- Convert Character to Numeric in R (With Examples)
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.