diff --git a/content/Practice.md b/content/Practice.md index 6d2bccc..7b83578 100644 --- a/content/Practice.md +++ b/content/Practice.md @@ -15,7 +15,7 @@ The idea is not to solve the probelm the same way that is here. The important th fruits <- c("one -pple", "two pe-rs", "three b-n-n-s") ```
Answer -fruits = str_replace_all(fruits, "-", "a") + fruits = str_replace_all(fruits, "-", "a")
### P2 - Replace "-" with "e" in the count column only @@ -27,7 +27,7 @@ count = c("on-", "two", "thr--") fruitcount = as.data.frame(cbind(fruit, count)) ```
Answer -fruitcount$count = gsub("-", "e", fruitcount$count) + fruitcount$count = gsub("-", "e", fruitcount$count)
## II - Groups of letters @@ -39,7 +39,7 @@ This is an example from stringr, type ?str_replace to find it in the help tab. fruits <- c("one apple", "two pears", "three bananas") ```
Answer -fruits = str_replace(fruits, "[aeiou]", "-") + fruits = str_replace(fruits, "[aeiou]", "-")
## III - Combinations of words @@ -69,13 +69,11 @@ Fix the herbarium_inventory to
Answer 1. Remove all spaces -

herbarium_inventory <- str_replace_all(" ", "", herbarium_inventory)

- +

herbarium_inventory <- str_replace_all(" ", "", herbarium_inventory)

2. Convert the "deposited" column to 1 for "yes" and 0 for "no" -

herbarium_inventory$deposited <- ifelse(grepl("y(es)", herbarium_inventory$deposited), 1, 0)

- +

herbarium_inventory$deposited <- ifelse(grepl("y(es)", herbarium_inventory$deposited), 1, 0)

3. Fix month -

herbarium_inventory$dates <- gsub("-(0?3-|(?i)mar)-", "-March-", herbarium_inventory$dates)

+

herbarium_inventory$dates <- gsub("-(0?3-|(?i)mar)-", "-March-", herbarium_inventory$dates)

@@ -90,7 +88,7 @@ count = c("one", "two", "three") fruitcount = as.data.frame(cbind(fruit, count)) ```
Answer -fruitcount = data.frame(lapply(fruitcount, gsub, pattern = "e", replacement = "-", fixed = TRUE)) + fruitcount = data.frame(lapply(fruitcount, gsub, pattern = "e", replacement = "-", fixed = TRUE))
### P3 - Edit URLs @@ -105,16 +103,13 @@ url = c("https://www2.gov.bc.ca/gov/content/home", Your target is to have this url list "gov.bc" "canada" "yukon" "alberta"
Answer -## get rid of the parts of the very start of the urls -url.short = gsub("https://", "", url) - -## get rid of the rest of the start start of URLs that is not interesting rigth now -url.short = gsub("(www|www2)(\\.)", "", url.short) -url.short -## get rid of .ca and everything at the end -url.short = gsub("\\.ca.*", "", url.short) -url.short +1. get rid of the parts of the very start of the urls +

url.short = gsub("https://", "", url)

+2. get rid of the rest of the start start of URLs that is not interesting rigth now +

url.short = gsub("(www|www2)(\\.)", "", url.short)

+3. get rid of .ca and everything at the end

+

url.short = gsub("\\.ca.*", "", url.short)