Built with Tutify.io

    AIStudyLabs

    Extract the Last N Characters from String in R

    Extract the Last N Characters from String in R

    To extract the last N characters from a string in R, you can use the base R functions substring() and substr(), or the str_sub() function from the stringr package. Extracting the last N characters is a common task in data manipulation and text processing. Each of these functions offers unique advantages and can be chosen based on the specific needs of your task. In this article, I will explain multiple methods for efficiently extracting the last N characters from a string using both base R functions and the stringr package.

    Key points-

    <a id="h-using-substring-extract-last-n-characters"></a>

    Using substring() Extract Last N Characters

    The substring() function in base R extracts the last N characters from a string. Here's how you can use it to extract the last n characters from a string.

    First, define a string and specify the number of characters to extract. Use the nchar() function to compute the length of the string, then subtract the nth character from this length (i.e., nchar(string) - nth character). Finally, add 1 to this calculation to get the desired result.

    
    # Extract last chracters of a string using substring()
    string <- "AIStudyLabs"
    n <- 8
    print("Extract the last n characters from the given string:")
    substring(string, nchar(string) - n + 1)
    

    Yields below output.

    <a id="h-using-substr-extract-the-last-n-characters-in-r"></a>

    Using substr() Extract the Last N Characters in R

    Alternatively, you can use the substr() function, another base R function, to extract the last N characters from a string. It takes three parameters: the string, the starting position for extracting the last N characters, and the ending position. It returns the specified substring from the string.

    
    string <- "AIStudyLabs"
    n <- 8
    print("Given string")
    print(string)
    
    print("Extract the last n characters from the given string:")
    substr(string, nchar(string) - n + 1, nchar(string))
    
    # Output:
    # [1] "Given string"
    # [1] "AIStudyLabs"
    # [1] "Extract the last n characters from the given string:"
    # [1] "Examples"
    

    <a id="h-using-stringr-str-sub-in-r"></a>

    Using stringr::str_sub() in R

    The str_sub() function from the stringr package offers a straightforward and reliable method for managing string operations. It allows to extract a substring starting from the nth character to the end of the string. To specify the substring from the end, you can place a negative sign before the nth character.

    
    # install stringr package
    install.packages("stringr")
    library(stringr)
    string <- "AIStudyLabs"
    n <- 8
    print("Given string")
    print(string)
    print("Extract the last n characters from the given string:")
    str_sub(string, -n)
    
    # Output:
    # [1] "Given string"
    # [1] "AIStudyLabs"
    # [1] "Extract the last n characters from the given string:"
    # [1] "Examples"
    

    From the above code, str_sub(string, -n) extracts the substring starting from the nth character from the end of the string. Given that n is 8, it retrieves the last 8 characters of AIStudyLabs, resulting in Examples.

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

    Conclusion

    In this article, I have explained that extracting the last N characters from a string in R can be efficiently achieved using various functions, including the base R functions substring() and substr(), as well as the str_sub() function from the stringr package.

    Happy learning!!