What are casefold(), toupper() & tolower() functions in R

In the R Programming language, the functions casefold(), toupper() & tolower() are string functions used to change the case of letters in a string.


The function casefold() can be used for both, changing upper case letters to lower case & lower case letters to upper case. Whereas toupper() can only change lowercase letters to uppercase and tolower() can only change uppercase letters to lowercase. These functions work for only alphabets. In case there is a non-alphabet character in the string, it will remain the same as it was before passing to the function.

casefold() Function in R

By default, casefold() act as the tolower() function, i.e., it changes the case of all uppercase letters in a string to lowercase letters.

Code Example 1 (Default Implementation)

x <- "WelCome to AlgoIdeas"  
x <- casefold(x)
print(x)
  • Line 1: Assigning a string containing both upper & lowercase letters.
  • Line 2: Invoking casefold() function on the string & storing the returned string in the original variable x.
  • Line 3: Printing the updated string on console.

Output

casefold function in r
casefold function in R

Moreover, along with the string argument, the casefold() function can accept one Boolean argument passed as casefold(name_of_string, upper=FALSE). If you pass upper=FALSE, it will act the same as tolower() function. But, if you want to capitalize the lowercase letters, you must pass upper=TRUE along with the string argument. The below example will help you better understand this function.

Code Example 2(Passing the Boolean argument)

x <- "Welcome to AlgoIdeas"  
x <- casefold(x, upper=TRUE)
print(x)
  • Line 1: Assigning a string containing both upper & lowercase letters.
  • Line 2: Invoking casefold() function on the string while setting the upper parameter to TRUE & storing the returned string in variable x.
  • Line 3: Printing the updated string on console.

Output

casefold function in R
casefold function in R

toupper() Function in R

The toupper() function takes a string as input and capitalizes all its Lowercase letters to Uppercase.

toupper function in r
toupper function in r
x <- toupper("WelCome to AlgoIdeas")
print(x)
  • Line 1: Passing a string containing both upper & lowercase letters to the toupper() function and assigning the returned value to variable x.
  • Line 2: Printing the returned string.

Output

toupper function in r
toupper function in R

tolower() Function in R

The tolower() function takes a string as input and changes the case of all its uppercase letters to lowercase letters.

tolower function in r
tolower function in r
x <- tolower("WelCome to AlgoIdeas")
print(x)
  • Line 1: Passing a string containing both upper & lowercase letters to the tolower() function and assigning the returned value to variable x.
  • Line 2: Printing the returned string.

Output

tolower function in R
tolower function in R

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...