[emaillocker]Below are the solutions to these exercises on stringr.
# load package library(stringr) # prepare the exercises data addresses <- c("14 Pine Street, Los Angeles", "152 Redwood Street, Seattle", "8 Washington Boulevard, New York") products <- c("TV ", " laptop", "portable charger", "Wireless Keybord", " HeadPhones ") long_sentences <- stringr::sentences[1:10] field_names <- c("order_number", "order_date", "customer_email", "product_title", "amount") employee_skills <- c("John Bale (Beginner)", "Rita Murphy (Pro)", "Chris White (Pro)", "Sarah Reid (Medium)") #################### # # # Exercise 1 # # # #################### str_to_lower(string = addresses)
## [1] "14 pine street, los angeles" "152 redwood street, seattle" ## [3] "8 washington boulevard, new york"
#################### # # # Exercise 2 # # # #################### str_extract(string = addresses, pattern = "[:digit:]+")
## [1] "14" "152" "8"
#################### # # # Exercise 3 # # # #################### str_split(string = addresses, pattern = ", ", simplify = T)
## [,1] [,2] ## [1,] "14 Pine Street" "Los Angeles" ## [2,] "152 Redwood Street" "Seattle" ## [3,] "8 Washington Boulevard" "New York"
#################### # # # Exercise 4 # # # #################### str_split(string = addresses, pattern = "(?<=[:digit:]) |, ", simplify = T)
## [,1] [,2] [,3] ## [1,] "14" "Pine Street" "Los Angeles" ## [2,] "152" "Redwood Street" "Seattle" ## [3,] "8" "Washington Boulevard" "New York"
#################### # # # Exercise 5 # # # #################### str_extract_all(string = long_sentences, pattern = "^T[A-z]+|[A-z]+s\\.$")
## [[1]] ## [1] "The" "planks." ## ## [[2]] ## character(0) ## ## [[3]] ## character(0) ## ## [[4]] ## [1] "These" ## ## [[5]] ## [1] "bowls." ## ## [[6]] ## [1] "The" ## ## [[7]] ## [1] "The" ## ## [[8]] ## [1] "The" ## ## [[9]] ## [1] "us." ## ## [[10]] ## character(0)
#################### # # # Exercise 6 # # # #################### str_trunc(string = long_sentences, width = 22, ellipsis = "..")
## [1] "The birch canoe slid.." "Glue the sheet to th.." ## [3] "It's easy to tell th.." "These days a chicken.." ## [5] "Rice is often served.." "The juice of lemons .." ## [7] "The box was thrown b.." "The hogs were fed ch.." ## [9] "Four hours of steady.." "Large size in stocki.."
#################### # # # Exercise 7 # # # #################### str_to_upper(string = str_squish(string = products))
## [1] "TV" "LAPTOP" "PORTABLE CHARGER" ## [4] "WIRELESS KEYBORD" "HEADPHONES"
#################### # # # Exercise 8 # # # #################### str_to_title(string = str_replace_all(string = field_names, pattern = "_", replacement = " "))
## [1] "Order Number" "Order Date" "Customer Email" "Product Title" ## [5] "Amount"
#################### # # # Exercise 9 # # # #################### str_pad(field_names, width = max(str_length(field_names)), side = "left")
## [1] " order_number" " order_date" "customer_email" " product_title" ## [5] " amount"
#################### # # # Exercise 10 # # # #################### str_match(string = employee_skills, pattern = "([A-z ]*) \\((Pro|Medium)\\)")[, 2:3]
## [,1] [,2] ## [1,] NA NA ## [2,] "Rita Murphy" "Pro" ## [3,] "Chris White" "Pro" ## [4,] "Sarah Reid" "Medium"
[/emaillocker]
Leave a Reply