Function substr() with R

Function substr()

The substr() function in R is used to extract or replace substrings from a character string. It allows you to specify the starting and ending positions of the substring you want to extract or replace.

Syntax 

substr(x, start, stop)

Arguments:

  • x:
    • The character string or vector of strings from which to extract or replace the substring.
    • Example: “Hello World”, c(“abc”, “def”).
  • start:
    • The starting position (1-based index) of the substring you want to extract or replace.
    • Example: 1 for the beginning of the string.
  • stop:
    • The ending position (1-based index) of the substring you want to extract or replace.
    • Example: 5 to end the substring at the fifth character.

Practical Examples

Example 1: Extracting a Substring 

# Extract a substring from position 1 to 5
substr("Hello World", start = 1, stop = 5)
# [1] "Hello"

Example 2: Extracting from a Vector of Strings 

# Extract a substring from position 2 to 4 for each element in a vector
substr(c("abcdef", "123456"), start = 2, stop = 4)
# [1] "bcd" "234"

Example 3: Replacing a Substring 

# Replace substring "World" with "R" in "Hello World"
s <- "Hello World"
substr(s, start = 7, stop = 11) <- "R"
# [1] "Hello R"

 Example 4: Handling Edge Cases 

# Extract a substring with start greater than stop
substr("Hello", start = 5, stop = 2)
# [1] ""
# Extract a substring with positions outside the string's range
substr("Hi", start = 1, stop = 10)
# [1] "Hi"

Points to Note

  • 1-Based Indexing: start and stop indices are 1-based, meaning the first character of the string is at position 1.
  • Empty Strings: If start is greater than stop, or if start or stop are outside the string’s range, the result is an empty string.
  • Vectorized Operation: substr() works with character vectors. If x is a vector, start, and stop should be of the same length as x, or be length 1 to apply to all elements.
  • Replacement: When using substr() to replace part of a string, ensure that the replacement string is of the same length as the substring being replaced.

Examples of Edge Cases 

# Substring start and stop out of bounds
substr("example", start = 10, stop = 15)
# [1] ""
# Start greater than stop
substr("example", start = 5, stop = 2)
# [1] ""

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print