How to Convert String to Uppercase in R

A character object in R represents the string values. To create a character object in R, use the as.character() function. A text is represented as a sequence of characters that may contain letters, numbers, and symbols. You can create a character string by surrounding text within double-quotes.

"wandavision"

Or you can also surround text within single quotes:

'wandavision'

Convert Lowercase String to Uppercase

To convert a lowercase string to an uppercase string in R, use the toupper() method. The toupper() method changes the case of a string to the upper. The toupper() function takes a string as an argument and returns the uppercase version of a string.

Syntax

toupper(s)

Parameters

The function takes s as a parameter.

Return value

It returns the upper case version of the string.

Example

data <- "wandavision"

converted <- toupper(data)

converted

Output

[1] "WANDAVISION"

And we get the string with all the uppercase letters.

Example 2

data <- "Rick and Morty are Vindicators"

converted <- toupper(data)

converted

Output

[1] "RICK AND MORTY ARE VINDICATORS"

casefold() Function in R

The casefold() function in R is a generalized solution to convert to either lowercase or uppercase letters. The casefold() method converts a character vector to upper or lower case, depending on the value of the parameter upper. If you pass upper = TRUE, then it will convert to uppercase, and if you pass upper = FALSE, then it will return the lowercase string.

Syntax

casefold(string, upper=F)

Parameters

  1. string = It is a string character or a character data frame.
  2. upper = It is a case of the string will be lower if mentioned FALSE, and it will be upper if mentioned TRUE.

Example

data <- "Rick and Morty are Vindicators"

converted <- casefold(data, upper = TRUE)

converted

Output

[1] "RICK AND MORTY ARE VINDICATORS"

You can see from the output that we got the uppercase characters string.

If we pass the upper = FALSE, then it will return the lowercase characters string.

data <- "Rick and Morty are Vindicators"

converted <- casefold(data, upper = FALSE)

converted

Output

[1] "rick and morty are vindicators"

And we converted all the letters to lowercase, bypassing upper = FALSE.

Conclusion

To convert character vectors or objects to uppercase use either the toupper() method or the casefold() method. The casefold() method translates characters in character vectors from the specific upper to lower case or vice versa.

Leave a Comment