lowercase in R: How to Convert String to Lowercase 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'

Read also: How to Convert String to Uppercase in R

lowercase in R

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

Syntax

tolower(s)

Parameters

The function takes s as a parameter.

Return value

It returns the lowercase version of the string.

Example

data <- "RICK IS LITERALLY A GOD"

converted <- tolower(data)

converted

Output

[1] "rick is literally a god"

And we get the string with all the lowercase letters.

Example 2

data <- "RICK AND MORTY ARE VINDICATORS"

converted <- tolower(data)

converted

Output

[1] "rick and morty are vindicators"

casefold() Function in R

The casefold() is a built-in R function that 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 = FALSE, then it will convert to lowercase letters, and if you pass upper = TRUE, then it will return the uppercase 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 that 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 = FALSE)

converted

Output

[1] "rick and morty are vindicators"

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

If we pass the upper = TRUE, then it will return the uppercase characters string.

Conclusion

To convert character vectors or objects to lowercase, use either the tolower() 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