Skip to content
Related Articles

Related Articles

Improve Article

Case conversion of a String in R Language – toupper(), tolower(), casefold() and cartr() Function

  • Last Updated : 26 May, 2020

toupper() function in R Language is used to convert the lowercase letters to uppercase.

Syntax: toupper(x)

Parameters:
x: character vector

Example:




# R program to illustrate 
# uppercase of  vectors 
  
# Create example character string
x <- "GeeksforGeeks"                   
toupper(x)
  
print(x)

Output:



GEEKSFORGEEKS

Here in the above code we have converted a example vector of characters to uppercase characters.

tolower() Function

tolower() function is used to convert the uppercase letters to lowercase.

Syntax: tolower(x)

Parameters:
x: character vector

Example:




# R program to illustrate 
# uppercase of  vectors 
  
# Create example character string
x <- "GeeksforGeeks" 
tolower(x)
  
print(x)

Output:

geeksforgeeks

casefold() Function

casefold() function takes character vector and boolean value as argument. This boolean value decides the conversion to upper and lower case.

Syntax: casefold(x, upper)



Parameters:
x: Character Vector
upper: Boolean value for case conversion

Example 1:




# Create example character string
x <- "GeeksforGeeks"
  
# Convert to lower case letters 
x <- casefold(x, upper = FALSE)  
  
print(x)            

Output:

"geeksforgeeks"

Here in the above code, the boolean value for the upper is set to be FALSE to convert the character vector to lowercase.

Example 2:




# Create example character string
x <- "GeeksforGeeks"
  
# Convert to upper case letters 
casefold(x, upper = TRUE)   
  
print(x)              

Output:

"GEEKSFORGEEKS"

Here in the above code, the boolean value for the upper is set to be TRUE to convert the character vector to Uppercase.

cartr() Function

cartr() function is used when we have to make some characters to upper case and some to lower case.

Syntax: cartr(x, old, new)

Parameters:
x: character vector
old: a character string specifying the characters to be translated.
new: a character string specifying the translations

Example:




# Create example character string
x <- "GeeksforGeeks"
  
# Translate to upper and lower case
chartr(old = "Geeks", new = "GeEkS", x)    

Output:

"GeEkSforGeeks"



My Personal Notes arrow_drop_up
Recommended Articles
Page :