Related Articles

Related Articles

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

"GeEkSforGeeks"
My Personal Notes arrow_drop_up
Recommended Articles
Page :