Built with Tutify.io

    AIStudyLabs

    Explain R tolower() Function with Examples

    Explain R tolower() Function with Examples

    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-

    <a id="h-the-tolower-function"></a>

    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.

    <a id="h-syntax-of-the-tolower-function"></a>

    Syntax of the tolower() function

    Following is the syntax of the tolower() function.

    
    # Syntax of tolower() function
    tolower(string_name)
    

    <a id="h-parameters"></a>

    Parameters

    The tolower() function in R takes a single parameter.

    <a id="h-return-value"></a>

    Return value

    The tolower() function returns a character string or vector with all characters converted to lowercase.

    <a id="h-convert-single-string-to-lower-using-r-tolower"></a>

    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.

    <a id="h-convert-character-vector-to-lower-using-r-tolower"></a>

    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.

    <a id="h-convert-character-list-to-lower"></a>

    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"
    

    <a id="h-use-r-tolower-to-convert-the-data-frame-to-lower"></a>

    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
    

    <a id="h-numeric-characters-to-lower"></a>

    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"
    

    <a id="h-special-characters-to-lower"></a>

    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"
    

    <a id="h-convert-empty-string-to-lower-case-using-r-tolower"></a>

    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] ""
    

    <a id="h-conclusion"></a>

    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!!