### Extension: Getting to know with what is substring
## create a data frame
df <- data.frame (team = c ("Ahmad", "Bachelot", "Charles", "Douglass", "Friedman"))
# view the data frame
df
## team
## 1 Ahmad
## 2 Bachelot
## 3 Charles
## 4 Douglass
## 5 Friedman
# string is something like a line, and the string in columns are the characters
# with the same position in each box (e.g.: they're all the first letter in that box)
################### Example 1: Extract characters between certain positons
# create a new column that contains characters between positions 2 and 5
df$between2_5 <- substring( df$team, first = 2, last = 5)
# view the resulting data frame
df
## team between2_5
## 1 Ahmad hmad
## 2 Bachelot ache
## 3 Charles harl
## 4 Douglass ougl
## 5 Friedman ried
################### Example 2: Extract first N characters
df$first3 <- substring(df$team, first = 1, last = 3)
# view the resulting data frame
df
## team between2_5 first3
## 1 Ahmad hmad Ahm
## 2 Bachelot ache Bac
## 3 Charles harl Cha
## 4 Douglass ougl Dou
## 5 Friedman ried Fri
################### Example 3: Extract last 3 characters
df$last3 <- substring(df$team, nchar(df$team) -3+1 , nchar(df$team))
# view the resulting data frame
df
## team between2_5 first3 last3
## 1 Ahmad hmad Ahm mad
## 2 Bachelot ache Bac lot
## 3 Charles harl Cha les
## 4 Douglass ougl Dou ass
## 5 Friedman ried Fri man
################### Example 4: Replace the substring
substring(df$team, first = 1, last = 3) <- "***"
# view the resulting data frame
df
## team between2_5 first3 last3
## 1 ***ad hmad Ahm mad
## 2 ***helot ache Bac lot
## 3 ***rles harl Cha les
## 4 ***glass ougl Dou ass
## 5 ***edman ried Fri man