lowercase in r [how to clean up the case of a string]

Sometimes when dealing with a character string in r programming you are going to have letters that are in the wrong case. As a result, you may need to convert the upper case alphabet to lowercase or vice versa. There is a way to correct the case of a letter in a string during runtime.

Description – lowercase in R

When you need to convert letters to lowercase in r, you use the tolower() function. This function has the form of tolower(s) where s is the string of characters that you want to convert to lowercase. The function is most often applied to variables. While you could apply this function to a string constant, it serves little practical purpose because you can just edit the text. It is a perfect tool for making sure that your textual data is all the same case. This knowledge comes in handy when you do not have control over how the data is entered.

Explanation – lowercase R

This function works by treating strings like a vector of characters and changing each individual character. It simply changes every letter of the alphabet into a lowercase letter. There exists a function that does the same thing for capital letters. In both functions, the cases of the letters are changed. Both functions change all the characters in the character string. These functions do not change the characters based on grammar, so this is not an automatic grammar correction tool. They will give you letters that are all the same case. They simply make sure that all the characters in the character string have the same case.

Developers Examples

Here we have three examples of this function in use.

> x = “A CAT AND DOG.”
> tolower(x)
[1] “a cat and dog.”

In this first example, the lower case letters all start out capitalized and you can see what the lowercase letter function does to them.

> x = toupper(“Love Programming In R.”)
> x
[1] “LOVE PROGRAMMING IN R.”
> y = paste(“I”,tolower(x))
> y
[1] “I love programming in r.”

In the second example, we start out with all uppercase letters. We then use the function to change their case and add a capital ā€œIā€ to make a proper sentence.

> x = “The QuiCk Brown FoX JUmpS oVer the LaZy DOg.”
> substring(x, 2)
[1] “he QuiCk Brown FoX JUmpS oVer the LaZy DOg.”
> substring(x, 2)=tolower(substring(x, 2))
> x
[1] “The quick brown fox jumps over the lazy dog.

In this final example, we start out with mixed cases and then use this function along with the substring() function, to produce a proper sentence.

Application of lowercase function in R

There are two main practical applications for this function. The first is to make sure that all the letters in a variable are the same case so that they can be compared with a standard. The second is to make sure that letters from sources that you do not have any control have the proper capitalization. These are most often the situation with data loaded in from an external file or entered by a user.

If you need to change the cases of the letters in a character string these two functions are the tools that you will use. The reverse operation, converting a string to capital letters, makes use of the toupper function. To make sure that all of the words are properly capitalized, you will have to use other functions but they do get the job done.

Scroll to top
Privacy Policy