Import your data

data <- read_excel("../00_data/myData.xlsx")
## New names:
## • `` -> `...1`
data
## # A tibble: 4,810 × 24
##     ...1  rank position hand  player   years total…¹ status yr_st…² season   age
##    <dbl> <dbl> <chr>    <chr> <chr>    <chr>   <dbl> <chr>    <dbl> <chr>  <dbl>
##  1     1     1 C        Left  Wayne G… 1979…     894 Retir…    1979 1978-…    18
##  2     2     1 C        Left  Wayne G… 1979…     894 Retir…    1979 1978-…    18
##  3     3     1 C        Left  Wayne G… 1979…     894 Retir…    1979 1978-…    18
##  4     4     1 C        Left  Wayne G… 1979…     894 Retir…    1979 1979-…    19
##  5     5     1 C        Left  Wayne G… 1979…     894 Retir…    1979 1980-…    20
##  6     6     1 C        Left  Wayne G… 1979…     894 Retir…    1979 1981-…    21
##  7     7     1 C        Left  Wayne G… 1979…     894 Retir…    1979 1982-…    22
##  8     8     1 C        Left  Wayne G… 1979…     894 Retir…    1979 1983-…    23
##  9     9     1 C        Left  Wayne G… 1979…     894 Retir…    1979 1984-…    24
## 10    10     1 C        Left  Wayne G… 1979…     894 Retir…    1979 1985-…    25
## # … with 4,800 more rows, 13 more variables: team <chr>, league <chr>,
## #   season_games <dbl>, goals <dbl>, assists <dbl>, points <dbl>,
## #   plus_minus <chr>, penalty_min <dbl>, goals_even <chr>,
## #   goals_power_play <chr>, goals_short_handed <chr>, goals_game_winner <chr>,
## #   headshot <chr>, and abbreviated variable names ¹​total_goals, ²​yr_start
data_small <- data %>% select(where(is.character)) %>% head(n = 40)
data_small
## # A tibble: 40 × 14
##    posit…¹ hand  player years status season team  league plus_…² goals…³ goals…⁴
##    <chr>   <chr> <chr>  <chr> <chr>  <chr>  <chr> <chr>  <chr>   <chr>   <chr>  
##  1 C       Left  Wayne… 1979… Retir… 1978-… TOT   WHA    20      NA      NA     
##  2 C       Left  Wayne… 1979… Retir… 1978-… INR   WHA    -3      3       0      
##  3 C       Left  Wayne… 1979… Retir… 1978-… EDO   WHA    23      34      9      
##  4 C       Left  Wayne… 1979… Retir… 1979-… EDM   NHL    14      37      13     
##  5 C       Left  Wayne… 1979… Retir… 1980-… EDM   NHL    41      36      15     
##  6 C       Left  Wayne… 1979… Retir… 1981-… EDM   NHL    80      68      18     
##  7 C       Left  Wayne… 1979… Retir… 1982-… EDM   NHL    61      47      18     
##  8 C       Left  Wayne… 1979… Retir… 1983-… EDM   NHL    78      55      20     
##  9 C       Left  Wayne… 1979… Retir… 1984-… EDM   NHL    100     54      8      
## 10 C       Left  Wayne… 1979… Retir… 1985-… EDM   NHL    71      38      11     
## # … with 30 more rows, 3 more variables: goals_short_handed <chr>,
## #   goals_game_winner <chr>, headshot <chr>, and abbreviated variable names
## #   ¹​position, ²​plus_minus, ³​goals_even, ⁴​goals_power_play

Chapter 14

Tools

Detect matches

data_small %>% summarise(sum(str_detect(team, "M$")))
## # A tibble: 1 × 1
##   `sum(str_detect(team, "M$"))`
##                           <int>
## 1                             9
str_detect(data_small$team, "M$")
##  [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [37] FALSE FALSE FALSE FALSE
sum(str_detect(data_small$team, "M$"))
## [1] 9
mean(str_detect(data_small$team, "M$"))
## [1] 0.225

Extract matches

Vowels <- c("A", "E", "I", "O", "U")
Vowel_match <- str_c(Vowels, collapse = "|")
Vowel_match
## [1] "A|E|I|O|U"
has_Vowels <- str_subset(sentences, Vowel_match)
has_Vowels
##   [1] "It's easy to tell the depth of a well."                  
##   [2] "A rod is used to catch pink salmon."                     
##   [3] "A pot of tea helps to pass the evening."                 
##   [4] "It snowed, rained, and hailed the same morning."         
##   [5] "A king ruled the state in the early days."               
##   [6] "Adding fast leads to wrong sums."                        
##   [7] "A saw is a tool used for making boards."                 
##   [8] "A cup of sugar makes sweet fudge."                       
##   [9] "Use a pencil to write the first draft."                  
##  [10] "A small creek cut across the field."                     
##  [11] "A yacht slid around the point into the bay."             
##  [12] "A tame squirrel makes a nice pet."                       
##  [13] "Always close the barn door tight."                       
##  [14] "A wisp of cloud hung in the blue air."                   
##  [15] "A pound of sugar costs more than eggs."                  
##  [16] "A tusk is used to make costly gifts."                    
##  [17] "Oak is strong and also gives shade."                     
##  [18] "Open the crate but don't break the glass."               
##  [19] "Add the sum to the product of these three."              
##  [20] "Act on these orders with great speed."                   
##  [21] "A lame back kept his score low."                         
##  [22] "It caught its hind paw in a rusty trap."                 
##  [23] "A cramp is no small danger on a swim."                   
##  [24] "A salt pickle tastes fine with ham."                     
##  [25] "A speedy man can beat this track mark."                  
##  [26] "At that high level the air is pure."                     
##  [27] "A filing case is now hard to buy."                       
##  [28] "An abrupt start does not win the prize."                 
##  [29] "A rag will soak up spilled water."                       
##  [30] "A shower of dirt fell from the hot pipes."               
##  [31] "Add the store's account to the last cent."               
##  [32] "Acid burns holes in wool cloth."                         
##  [33] "Eight miles of woodland burned to waste."                
##  [34] "A young child should not suffer fright."                 
##  [35] "Add the column and put the sum here."                    
##  [36] "A blue crane is a tall wading bird."                     
##  [37] "A fresh start will work such wonders."                   
##  [38] "After the dance they went straight home."                
##  [39] "Even the worst will beat his low score."                 
##  [40] "It is hard to erase blue or red ink."                    
##  [41] "A pencil with black lead writes best."                   
##  [42] "A waxed floor makes us lose balance."                    
##  [43] "On the islands the sea breeze is soft and mild."         
##  [44] "Add salt before you fry the egg."                        
##  [45] "In some forin or other we need fun."                     
##  [46] "A dash of pepper spoils beef stew."                      
##  [47] "A zestful food is the hot-cross bun."                    
##  [48] "A joy to every child is the swan boat."                  
##  [49] "All sat frozen and watched the screen."                  
##  [50] "It is late morning on the old wall clock."               
##  [51] "A gem in the rough needs work to polish."                
##  [52] "A castle built from sand fails to endure."               
##  [53] "A child's wit saved the day for us."                     
##  [54] "Each penny shone like new."                              
##  [55] "A ripe plum is fit for a king's palate."                 
##  [56] "Our plans right now are hazy."                           
##  [57] "It takes a good trap to capture a bear."                 
##  [58] "It takes a lot of help to finish these."                 
##  [59] "A sash of gold silk will trim her dress."                
##  [60] "A siege will crack the strong defense."                  
##  [61] "A lathe cuts and trims any wood."                        
##  [62] "It's a dense crowd in two distinct ways."                
##  [63] "It takes heat to bring out the odor."                    
##  [64] "A pod is what peas always grow in."                      
##  [65] "Once we stood beside the shore."                         
##  [66] "A chink in the wall allowed a draft to blow."            
##  [67] "A cold dip restores health and zest."                    
##  [68] "A gray mare walked before the colt."                     
##  [69] "A clean neck means a neat collar."                       
##  [70] "A fur muff is stylish once more."                        
##  [71] "A fence cuts through the corner lot."                    
##  [72] "A quart of milk is water for the most part."             
##  [73] "In the rear of the ground floor was a large passage."    
##  [74] "A man in a blue sweater sat at the desk."                
##  [75] "Oats are a food eaten by horse and man."                 
##  [76] "A force equal to that would move the earth."             
##  [77] "A bowl of rice is free with chicken stew."               
##  [78] "A big wet stain was on the round carpet."                
##  [79] "A rich farm is rare in this sandy waste."                
##  [80] "A strong bid may scare your partner stiff."              
##  [81] "Even a just cause needs power to win."                   
##  [82] "A thing of small note can cause despair."                
##  [83] "A thick coat of black paint covered all."                
##  [84] "Either mud or dust are found at all times."              
##  [85] "If you mumble your speech will be lost."                 
##  [86] "At night the alarm roused him from a deep sleep."        
##  [87] "It matters not if he reads these words or those."        
##  [88] "A brown leather bag hung from its strap."                
##  [89] "A toad and a frog are hard to tell apart."               
##  [90] "A white silk jacket goes with any shoes."                
##  [91] "A break in the dam almost caused a flood."               
##  [92] "A round hole was drilled through the thin board."        
##  [93] "A vent near the edge brought in fresh air."              
##  [94] "It is a band of steel three inches wide."                
##  [95] "It was hidden from sight by a mass of leaves and shrubs."
##  [96] "Every word and phrase he speaks is true."                
##  [97] "A sullen smile gets few friends."                        
##  [98] "A stiff cord will do to fasten your shoe."               
##  [99] "A plea for funds seems to come again."                   
## [100] "A thin stripe runs down the middle."                     
## [101] "A six comes up more often than a ten."                   
## [102] "A steep trail is painful for our feet."                  
## [103] "A whiff of it will cure the most stubborn cold."         
## [104] "A cruise in warm waters in a sleek yacht is fun."        
## [105] "A streak of color ran down the left edge."               
## [106] "It was done before the boy could see it."                
## [107] "The rarest spice comes from the far East."               
## [108] "A smatter of French is worse than none."                 
## [109] "A round mat will cover the dull spot."                   
## [110] "A flat pack takes less luggage space."                   
## [111] "A stuffed chair slipped from the moving van."            
## [112] "A thin book fits in the side pocket."                    
## [113] "Our troops are set to strike heavy blows."               
## [114] "It was a bad error on the part of the new judge."        
## [115] "One step more and the board will collapse."              
## [116] "Open your book to the first page."                       
## [117] "A gold ring will please most any girl."                  
## [118] "A pink shell was found on the sandy beach."              
## [119] "A severe storm tore down the barn."

Replacing matches

data_small %>% mutate(team_chg = team %>% str_replace("^[A-Z]", "X"))
## # A tibble: 40 × 15
##    posit…¹ hand  player years status season team  league plus_…² goals…³ goals…⁴
##    <chr>   <chr> <chr>  <chr> <chr>  <chr>  <chr> <chr>  <chr>   <chr>   <chr>  
##  1 C       Left  Wayne… 1979… Retir… 1978-… TOT   WHA    20      NA      NA     
##  2 C       Left  Wayne… 1979… Retir… 1978-… INR   WHA    -3      3       0      
##  3 C       Left  Wayne… 1979… Retir… 1978-… EDO   WHA    23      34      9      
##  4 C       Left  Wayne… 1979… Retir… 1979-… EDM   NHL    14      37      13     
##  5 C       Left  Wayne… 1979… Retir… 1980-… EDM   NHL    41      36      15     
##  6 C       Left  Wayne… 1979… Retir… 1981-… EDM   NHL    80      68      18     
##  7 C       Left  Wayne… 1979… Retir… 1982-… EDM   NHL    61      47      18     
##  8 C       Left  Wayne… 1979… Retir… 1983-… EDM   NHL    78      55      20     
##  9 C       Left  Wayne… 1979… Retir… 1984-… EDM   NHL    100     54      8      
## 10 C       Left  Wayne… 1979… Retir… 1985-… EDM   NHL    71      38      11     
## # … with 30 more rows, 4 more variables: goals_short_handed <chr>,
## #   goals_game_winner <chr>, headshot <chr>, team_chg <chr>, and abbreviated
## #   variable names ¹​position, ²​plus_minus, ³​goals_even, ⁴​goals_power_play
data_small %>% mutate(team_chg = team %>% str_replace_all ("[A-Z]", "X"))
## # A tibble: 40 × 15
##    posit…¹ hand  player years status season team  league plus_…² goals…³ goals…⁴
##    <chr>   <chr> <chr>  <chr> <chr>  <chr>  <chr> <chr>  <chr>   <chr>   <chr>  
##  1 C       Left  Wayne… 1979… Retir… 1978-… TOT   WHA    20      NA      NA     
##  2 C       Left  Wayne… 1979… Retir… 1978-… INR   WHA    -3      3       0      
##  3 C       Left  Wayne… 1979… Retir… 1978-… EDO   WHA    23      34      9      
##  4 C       Left  Wayne… 1979… Retir… 1979-… EDM   NHL    14      37      13     
##  5 C       Left  Wayne… 1979… Retir… 1980-… EDM   NHL    41      36      15     
##  6 C       Left  Wayne… 1979… Retir… 1981-… EDM   NHL    80      68      18     
##  7 C       Left  Wayne… 1979… Retir… 1982-… EDM   NHL    61      47      18     
##  8 C       Left  Wayne… 1979… Retir… 1983-… EDM   NHL    78      55      20     
##  9 C       Left  Wayne… 1979… Retir… 1984-… EDM   NHL    100     54      8      
## 10 C       Left  Wayne… 1979… Retir… 1985-… EDM   NHL    71      38      11     
## # … with 30 more rows, 4 more variables: goals_short_handed <chr>,
## #   goals_game_winner <chr>, headshot <chr>, team_chg <chr>, and abbreviated
## #   variable names ¹​position, ²​plus_minus, ³​goals_even, ⁴​goals_power_play