The tolower()
function in R is used to convert all characters in a given string or character vector to lowercase. It is a powerful tool for text manipulation, transforming all alphabetic characters in the input to their lowercase equivalents while leaving non-alphabetic characters (such as numbers and special symbols) unchanged. In this article, I will explain the tolower()
function, including its syntax, parameters, and usage, and show how to convert R objects containing uppercase characters to lowercase with examples.
Key points-
tolower()
function transforms all characters in a string to lowercase.- This function takes a single parameter, which is the string to be converted.
- It returns a string with all characters in lowercase.
tolower()
can be directly applied to character vectors to convert all elements to lowercase.- If the input is a list or a data frame, you need to apply the function to each element or column respectively using functions like
lapply()
. - Numeric characters remain unchanged when passed through
tolower()
. - Special characters also remain unchanged.
- An empty string remains unchanged when passed through
tolower()
.
The tolower() Function
The tolower()
function accepts character strings or character vectors directly as input arguments and returns them in lowercase. When converting a list of strings or character vector columns of a data frame to lowercase using this function, you can use the lapply()
function to apply tolower()
to each element of these objects.
Syntax of the tolower() function
Following is the syntax of the tolower() function.
# Syntax of tolower() function
tolower(string_name)
Parameters
The tolower()
function in R takes a single parameter.
string_name
: Convert this character string or vector to lowercase. It can be a single string, a character vector, or even a column of a data frame.
Return value
The tolower()
function returns a character string or vector with all characters converted to lowercase.
Convert Single String to Lower using R tolower()
Let’s create the character string containing uppercase letters and pass it to the tolower()
function which converts all characters in the string to lowercase.
# Convert character string to lower
# Create a string
my_str <- "SPARK BY EXAMPLES"
print("Given string:")
print(my_str)
result <- tolower(my_str)
print("After converting a string to lowercase:")
print(result)
Yields below output.
Convert Character Vector to Lower using R tolower()
To convert a character vector to lowercase, you can use the tolower()
function. This function takes the character vector as an argument and converts all the strings in the vector to lowercase.
# Convert character vector to lower
# Create a character vector
my_str <- c("SPARK", "BY", "EXAMPLES")
print("Given vector:")
tolower(my_str)
print("After converting a vector to lower case:")
print(my_str)
Yields below output.
Convert Character List to Lower
Alternatively, you can use this function to convert a list of strings to lowercase. When converting a list to lowercase, tolower()
cannot be directly applied to the list. Instead, you need to use lapply()
to apply tolower()
to each element within the list.
Let’s create a list containing uppercase strings and pass it to the lapply() function along with the tolower()
function. The lapply()
function applies tolower()
to each element of the list and returns a list of the same length with all elements converted to lowercase.
# Convert character list to lower
# Create a list
char_list <- list("SPARK", "BY", "EXAMPLES")
print("Given list:")
print(char_list)
result <- lapply(char_list, tolower)
print("After converting a list of strings to lowercase:")
print(result)
# Output:
# [1] "Given list:"
# [[1]]
# [1] "SPARK"
# [[2]]
# [1] "BY"
# [[3]]
# [1] "EXAMPLES"
# [1] "After converting a list of strings to lowercase:"
# [[1]]
# [1] "spark"
# [[2]]
# [1] "by"
# [[3]]
# [1] "examples"
Use R tolower() to Convert the Data Frame to Lower
Similarly, you can use the tolower()
function to convert each column of a data frame to lowercase. Like with lists, the tolower()
function cannot be directly applied to data frames. Instead, you can use the lapply()
function to apply tolower()
to each column of the data frame.
Let’s create a data frame with two columns containing uppercase strings and apply the tolower()
function to each column using the lapply()
function. This will convert all column values to lowercase.
# Apply tolower() to each column in the data frame
# Create data frame
char_df <- data.frame(col1 = c("SPARK", "BY", "EXAMPlES"), col2 = c("IS", "A", "GOOD WEBSITE"))
print("Given data frame:")
print(char_df)
result <- data.frame(lapply(char_df, tolower), stringsAsFactors = FALSE)
print("After converting a data frame to lowercase:")
print(result)
# Output:
# [1] "Given data frame:"
# col1 col2
# 1 SPARK IS
# 2 BY A
# 3 EXAMPlES GOOD WEBSITE
# [1] "After converting a data frame to lowercase:"
# col1 col2
# 1 spark is
# 2 by a
# 3 examples good website
Numeric Characters to Lower
When we convert a string containing numeric characters to lowercase using the tolower()
function, the numeric characters will remain unchanged and will be returned as they were in the input.
# Convert numeric character to lower
# Create a string
my_str <- "12345"
print("Given string:")
print(my_str)
result <- tolower(my_str)
print("After converting a string to lowercase:")
print(result)
# Output:
# [1] "Given string:"
# [1] "12345"
# [1] "After converting a string to lowercase:"
# [1] "12345"
Special Characters to Lower
To convert a string containing special characters to lowercase using this function, it converts the alphabetic characters to lowercase but leaves the special characters unchanged.
# Convert special characters to lower
# Create astring
my_str <- "SPARK@#BY$*EXAMPLES"
print("Given string:")
print(my_str)
result <- tolower(my_str)
print("After converting a string to lowercase:")
print(result)
# Output:
# [1] "Given string:"
# [1] "SPARK@#BY$*EXAMPLES"
# [1] "After converting a string to lowercase:"
# [1] "spark@#by$*examples"
Convert Empty String to Lower Case using R tolower()
Finally, you can convert an empty string to a lowercase using the tolower()
function. The result will be the same as the input, which is an empty string.
# Convert empty string to lower
# Create astring
my_str <- ""
print("Given string:")
print(my_str)
result <- tolower(my_str)
print("After converting a string to lowercase:")
print(result)
# Output:
# [1] "Given string:"
# [1] ""
# [1] "After converting a string to lowercase:"
# [1] ""
Conclusion
In this article, I have explained that the tolower()
function in R is a powerful tool for converting character strings or character vectors to lowercase. I also discussed how to use the lapply()
function to apply tolower()
to other objects, such as lists and data frames, to convert their elements to lowercase.
Happy Learning!!