Hello, folks. In this tutorial we are going to convert the case of the string in R. The R language offers functions such as tolower(), toupper(), and casefold() to convert the case of the given string from upper to lower or vice versa as well.
Let’s see how these functions work.
Functions used for case conversion in R
tolower() = The tolower() function is used to convert the string characters to the lower case.
toupper() = The toupper() function is used to convert the string characters to upper case.
casefold() = The casefold function is used to convert the string characters from lower to upper case and vice versa as well.
Let’s start with the syntax
- The syntax of the function tolower() is,
1 |
tolower(x) |
x = string or a character vector
- The syntax of the toupper() function is,
1 |
toupper(x) |
x = string or a character vector
Convert a string to lower case using tolower() function
In this section, we are going to convert a string or a character vector to a lower case using tolower() function.
1 |
tolower("WWW.JOURNALDEV.COM") |
Output = “www.journaldev.com”
1 2 |
df<-c("HAVE A GOOD DAY") tolower(df) |
Output = “have a good day”
The tolower() function will convert the case of the characters to lower as shown above.
Now, let’s convert the case of the characters present in a data set ‘state.division’.
Let’s see how tolower() function converts the case of the string to lower case.
1 |
tolower(state.division) |
In the above output you can see that all the strings present in the column species get converted into small letters or lower cases.
Convert a string to upper case using toupper() function
In the above sections we have converted the strings to lower case. Now let’s convert the strings to upper case using toupper() function in R.
1 |
toupper("www.journaldev.com") |
Output = “WWW.JOURNALDEV.COM”
1 |
toupper("have a good day") |
Output = “HAVE A GOOD DAY”
These are just a few examples of the toupper() function. It converts the lower case string characters to upper case in R.
Let’s convert the string characters of a data frame to the upper case using toupper() function in R.
1 |
toupper(state.region) |
In the below output you can see how toupper() function converts the string characters of the data frame to upper case.
Convert a string to upper and lower case using casefold() function in R
Till now, we have come across tolower() and toupper() functions to convert the case of the strings to lower and upper cases respectively.
R offers another function named casefold(), which converts the string characters to lower and upper case.
The syntax:
1 |
casefold(x,upper=F) |
Where,
- x = string character or a character data frame.
- upper = case of the string will be lower if mentioned False and it will be upper if mentioned True.
Let’s see how the casefold() function converts the case of the strings.
1 |
casefold("i love r programming",upper = T) |
Output = “I LOVE R PROGRAMMING”
1 |
casefold("I LOVE R PROGRAMMING",upper=F) |
Output = “i love r programming”
Like this the casefold() function will convert the case of the string characters. Now, let’s see how casefold() function will convert the case of the data frame.
The below image shows the state.name data frame.
Let’s make use of casefold() function to convert the strings present in the data frame to upper case.
1 |
casefold(state.name,upper = T) |
In this way, you can convert the case of the strings in R using tolower(), toupper(), and casefold() function.
Wrapping up
R offers some functions to convert the case of the string characters present in a vector or a data frame.
This tutorial focussed on the case conversion using some specific functions in R.
That’s all for case conversion in R. Stay tuned for more updates on R programming tutorials. Happy conversion!!!