The substring() function is used to extract substrings in a character vector.
The following method shows how you can do it with syntax.
Method 1: Use substring() Function
substring(data,first=x,last=y)
data : A character vector x : Starting position of the substring y: Ending position of the substring
The following example shows how to use substring() method.
Get Substring Using substring() Function
We can use following example to get substring from character vector using substring() function:
# Create character vector
my_str <- "This is R language"
# Extract substring using substring()
substring(my_str, 2, 6)
Output:
[1] "his i"
In these example we use substring() function to extract substring from character vector and extract characters between 2 and 6 position.