Case conversion of a String in R Language – toupper(), tolower(), casefold() and cartr() Function
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 <- "w3wiki" toupper (x) print (x) |
Output:
w3wiki
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 <- "w3wiki" tolower (x) print (x) |
Output:
w3wiki
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 <- "w3wiki" # Convert to lower case letters x <- casefold (x, upper = FALSE ) print (x) |
Output:
"w3wiki"
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 <- "w3wiki" # Convert to upper case letters casefold (x, upper = TRUE ) print (x) |
Output:
"w3wiki"
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 <- "w3wiki" # Translate to upper and lower case chartr (old = "Beginner" , new = "GeEkS" , x) |
Output:
"GeEkSforBeginner"
Contact Us