How to convert the case of a string in R?

Filed Under: R Programming
case conversion in r

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,
tolower(x)     

x = string or a character vector

  • The syntax of the toupper() function is,
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.

tolower("WWW.JOURNALDEV.COM")

Output = “www.journaldev.com”

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’.

Lower In R

Let’s see how tolower() function converts the case of the string to lower case.

tolower(state.division)
Lower

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.

toupper("www.journaldev.com")

Output = “WWW.JOURNALDEV.COM”

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.

Toupper In R 1 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.

Upp 1

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:

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.

casefold("i love r programming",upper = T) 

Output = “I LOVE R PROGRAMMING”

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.

Casefold

Let’s make use of casefold() function to convert the strings present in the data frame to upper case.

casefold(state.name,upper = T)
Casefold 1

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!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages