Ahmed Muhammad
ECO 5380
April 20th, 2020
Use str_length() and str_sub() to extract the middle character from a string. What will you do if the string has an even number of characters?
Here an example of using str_length() and str_sub() to extract the middle character from a string with an odd number of letters. ‘m’ is the middle character in “ahmed”.
str_sub(strOdd, (str_length(strOdd)/2) + .5 , (str_length(strOdd)/2) + .5)
## [1] "m"
If there is an even number, I will take the two letters in the middle. ‘BU’ are the middle characters in “ahmedButEven”.
str_sub(strE, (str_length(strE)/2) , -(str_length(strE)/2) )
## [1] "Bu"
Looking for the number of instances in stringr::words for matches that:
Start with “y”:
length(str_subset(wordz, "^y"))
## [1] 6
End with “ed” but not with “eed”:
length(str_subset(wordz, "[^e]ed$"))
## [1] 3
End with “ing” or “ise”:
length(str_subset(wordz, "(ing|ise)$"))
## [1] 17
Are exactly three letters long:
length(str_subset(wordz, "(^[a-z]{3}$)"))
## [1] 110
Have seven letters or more:
length(str_subset(wordz, "([a-zA-z]{7})"))
## [1] 219
Have three or more vowels in a row:
length(str_subset(wordz, "[aeiou]{3}"))
## [1] 6
Start and end with the same character:
length(str_subset(wordz, "^([A-Za-z]).*\\1$"))
## [1] 36