The “substr” 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):

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

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

    • stop: Ending position(s) of the substring(s) to be extracted or replaced.

  • 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 <- substr(sentence, start = 5, stop = 9)

    print(extracted_substring)

  • In this example, substr 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 x, start, and stop arguments are required, and there are no additional optional arguments. The result is a character vector with the extracted substring.

Previous
Previous

The “trimws” Function in R

Next
Next

The “nchar” Function in R