Home » How to Use str_sub in R (With Examples)

How to Use str_sub in R (With Examples)

by Erma Khan

The str_sub() function from the stringr package in R can be used to extract or replace substrings in a string.

This function uses the following syntax:

str_sub(string, start, end)

where:

  • string: Character vector
  • start: Position of the first character
  • end: Position of the last character

This tutorial provides several examples of how to use this function in practice with the following data frame:

#create data frame
df frame(team=c('team_A', 'team_B', 'team_C', 'team_D'),
                 conference=c('West', 'West', 'East', 'East'),
                 points=c(88, 97, 94, 104))

#view data frame
df

    team conference points
1 team_A       West     88
2 team_B       West     97
3 team_C       East     94
4 team_D       East    104

Example 1: Extract Substring in a String

The following code shows how to extract the substring that starts in position 5 and ends in position 6 for each string in the “team” column:

library(stringr)

#extract characters in positions 5 through 6 of 'team' column
str_sub(string=df$team, start=5, end=6)

[1] "_A" "_B" "_C" "_D"

Example 2: Extract Substring Up to Specific Position

The following code shows how to extract every character up to position 4 for each string in the “team” column:

library(stringr)

#extract all characters up to position 4 in 'team' column
str_sub(string=df$team, end=4)

[1] "team" "team" "team" "team"

Example 3: Extract Substring Starting at Specific Position

The following code shows how to extract every character after position 3 for each string in the “team” column:

library(stringr)

#extract all characters after position 2 in 'team' column
str_sub(string=df$team, start=3)

[1] "am_A" "am_B" "am_C" "am_D"

Example 4: Replace Substring in a String

The following code shows how to replace the substring starting at position 1 and ending at position 5 for each string in the “team” column:

library(stringr)

#replace all characters between position 1 and 5 in 'team' column
str_sub(string=df$team, start=1, end=5) #view updated data frame
df

   team conference points
1 TEAMA       West     88
2 TEAMB       West     97
3 TEAMC       East     94
4 TEAMD       East    104

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use str_replace in R
How to Use str_split in R
How to Use str_detect in R

Related Posts