Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see case conversion of a string in R Programming Language.

R – toupper()

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

Syntax: toupper(x) 

Parameters: 

x: character vector

Example: 

R




# 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 an example vector of characters to uppercase characters.

R – tolower() Function

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

Syntax: tolower(x)

Parameters: 

x: character vector

Example:

R




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


Output:

geeksforgeeks

R – 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: 

R




# 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: 

R




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

R – cartr() Function

cartr() function in R Programming 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: 

R




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


Output: 

"GeEkSforGeeks"


Last Updated : 23 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads