
Notice that the new column contains the first three characters of the “team” column. The following code shows how to use the substring() function to extract the first 3 characters of the “team” column: #create new column that contains first 3 charactersĭf$first3 <- substring(df$team, first= 1, last= 3) Notice that the new column contains the characters between positions 2 and 5 of the “team” column. The following code shows how to use the substring() function to extract the characters between positions 2 and 5 of the “team” column: #create new column that contains characters between positions 2 and 5ĭf$between2_5 <- substring(df$team, first= 2, last= 5) frame(team=c('Mavericks', 'Hornets', 'Rockets', 'Grizzlies'))Įxample 1: Extract Characters Between Certain Positions The examples in this tutorial show how to use the substring() function in practice with the following data frame in R: #create data frameĭf <- data.

start: The first element to be extracted.first: The first element to be extractedĪlso note that the substr() function does the exact same thing, but with slightly different argument names: substr(text, first, last).This function uses the following syntax: substring(text, first, last) The substring() function in R can be used to extract a substring in a character vector.
