Extracting Substrings from a Character Vector in R Programming – substring() Function

substring() function in R Language is used to extract substrings in a character vector.

Syntax: substring(text, first, last)

Parameters:
text: character vector
first: integer, the first element to be replaced
last: integer, the last element to be replaced

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# substring function
  
# Calling substring() function
substring("Geeks", 2, 3)
substring("Geeks", 1, 4)
substring("GFG", 1, 1)
substring("gfg", 3, 3)

chevron_right


Output :

[1] "ee"
[1] "Geek"
[1] "G"
[1] "g"

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# substring function
  
# Initializing a string vector
x <- c("GFG", "gfg", "Geeks")
  
# Calling substring() function
substring(x, 2, 3)
substring(x, 1, 3)
substring(x, 2, 2)

chevron_right


Output:

[1] "FG" "fg" "ee"
[1] "GFG" "gfg" "Gee"
[1] "F" "f" "e"
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.