The “substring” Function in R

  • Package: Base R (No specific package, it’s a built-in function)

  • Purpose: To extract or replace substrings from a character vector.

  • General Class: String Manipulation

  • Required Argument(s):

    • text: A character vector from which to extract or replace substrings.

    • first: Starting position(s) of the substring(s) to be extracted or replaced.

    • last: Ending position(s) of the substring(s) to be extracted or replaced. The default is 1000000.

  • Notable Optional Arguments:

    • None

  • Example:

  • # Example usage
    sentence <- "The quick brown fox jumps over the lazy dog."

    # Extract a substring from position 5 to 9
    extracted_substring <- substring(sentence, first = 5, last =9)

    print(extracted_substring)

  • In this example, substring is a built-in function in base R, and it is used to extract a substring from a character vector (sentence) starting from position 5 to position 9. The text, first, and last arguments are required, and there are no additional optional arguments. The result is a character vector with the extracted substring.

Previous
Previous

The “sprintf” Function in R

Next
Next

The “toupper” Function in R