Chapter 14 Strings - R for Data Science

Thi

9/20/2019


## -- Attaching packages ---------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.1     v readr   1.3.1
## v tibble  2.1.3     v purrr   0.3.2
## v tidyr   0.8.3     v dplyr   0.8.3
## v ggplot2 3.2.1     v forcats 0.4.0
## -- Conflicts ------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

14.2.5 Q1 In code that doesn’t use stringr, you’ll often see paste() and paste0(). What’s the difference between the two functions? What stringr function are they equivalent to? How do the functions differ in their handling of NA

[1] “today is”

[1] “todayis”

[1] “todayis”

paste0 is similiar to str_c

[1] “today is NA”

[1] “todayisNA”

[1] NA

paste handle NA like a object

paste0 handle NA like character

str_c propagates NA, if any values is missing, the function returns NA

14.2.5 Q2 In your own words, describe the difference between the sep and collapse arguments to str_c().

[1] “today, is”

[1] “todayis”

sep can be used to add character together and separate them by special character.

[1] “today|is|a”

collapse cannot be use to paste charater together, it can only add special character between strings in a list.

14.2.5 Q3 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?

[1] 3

[1] “d”

[1] “a”

If string has a even number of chracter, get the “right” middle character. For example: “dean” -> get “a”

[1] 12

[1] 14

[1] 14

On a side not, function round() would round (0.5) to nearest even number. It is not a good function to use for calculating grades, or business transactions. Look at: floor(), signif(), ceiling(), trunc() for more options.

14.2.5 Q4 What does str_wrap() do? When might you want to use it?

[1] " Cincoro, a new high-end Tequila helmed by a teamof NBA owners—including Michael Jordan—and financeprofessionals, has launched in 12 markets acrossthe U.S. The new entry is taking aim at the luxurymarket in the on- and off-premise with packagingdesigned by Nike’s Mark Smith and liquid selectedby the brand’s founders in consultation withindustry experts."

                Cincoro, a new high-end Tequila helmed by a team
                of NBA owners—including Michael Jordan—and finance
                professionals, has launched in 12 markets across
                the U.S. The new entry is taking aim at the luxury
                market in the on- and off-premise with packaging
                designed by Nike’s Mark Smith and liquid selected
                by the brand’s founders in consultation with
                industry experts. 

str_wrap deal with very long text, it cut long text into smaller defined length (width = ).

14.2.5 Q5 What does str_trim() do? What’s the opposite of str_trim()?

[1] “today is a”

[1] “today is a”

[1] " today is a"

str_trim trim space in text Opposite of str_trim is str_pad. Add space to text

[1] " today is " 14.2.5 Q6 Write a function that turns (e.g.) a vector c(“a”, “b”, “c”) into the string a, b, and c. Think carefully about what it should do if given a vector of length 0, 1, or 2.

vec_to_str <- function(x) { string = paste(x,‘,’) string}

[1] “a” “g” “d”

[1] “a g and d”

14.3.1 Q2 How would you match the sequence "’ ?

[1] “abc” “a.c” “"’\\”

14.3.1 Q3 What patterns will the regular expression ...... match? How would you represent it as a string?

[1] “\..\..\..”

It matches “.” something “.” something “.” For example .Q.W. or .as.fgfg.

14.3.2 Q1 How would you match the literal string “\(^\)

[1] “abc” “a.c” “\(^\)

14.3.2 Q2 Given the corpus of common words in stringr::words , create regular expressions that find all words that:

Start with “y”.

End with “x”

Are exactly three letters long.

Have seven letters or more.

14.3.3.1 Q1 Create regular expressions to find all words that:

Start with a vowel.

That only contain consonants. (Hint: thinking about matching “not”-vowels.)

End with ed, but not with eed.

End with ing or ise.

14.3.3.1 Q2 Empirically verify the rule “i before e except after c”.

14.3.3.1 Q3 Is “q” always followed by a “u”?

NO

14.3.3.1 Q4 Write a regular expression that matches a word if it’s probably written in British English, not American English.

colour

14.3.3 Q5 Create a regular expression that will match telephone numbers as commonly written in your country.

14.3.4 Q2 Describe in words what these regular expressions match: (read carefully to see if I’m using a regular expression or a string that defines a regular expression.)

^.*$ Match any string “\{.+\}” Match any string with {} -- match dddd-dd-dd (digits) “\\{4}” match \\

14.3.4 Q3 Create regular expressions to find all words that: Start with three consonants.

Have three or more vowels in a row.

Have two or more vowel-consonant pairs in a row

14.3.5 Q1 Describe, in words, what these expressions will match: (.)\1\1 : repeating of a charater : “aaa”, “bbb”, “ppp”, “rrr” “(.)(.)\2\1” “noon”, “appa”, “lool”, “tyyt” (..)\1 repeat of a pair of characters : “emem”, “anan” “popo” “(.).\1.\1” a character, any charater, the first character repeat, any charater, the first charater repeat again: “ataya”, “erehe”, “dedad” "(.)(.)(.).*\3\2\1 : any charater, any charater, any charater, many charater, repeat 3rd charater, repeat 2nd charater, repeat 1st charater: example

14.3.5 Q2 Construct regular expressions to match words that: Start and end with the same character.

Contain a repeated pair of letters (e.g. “church” contains “ch” repeated twice.)

Contain one letter repeated in at least three places (e.g. “eleven” contains three “e”s.)

14.4.1 Q1 For each of the following challenges, try solving it by using both a single regular expression, and a combination of multiple str_detect() calls.

Find all words that start or end with x.

OR

[1] “box” “sex” “six” “tax”

Find all words that start with a vowel and end with a consonant.

[1] “about” “accept” “account” “across” “act”
[6] “actual” “add” “address” “admit” “affect”
[11] “afford” “after” “afternoon” “again” “against”
[16] “agent” “air” “all” “allow” “almost”
[21] “along” “already” “alright” “although” “always”
[26] “amount” “and” “another” “answer” “any”
[31] “apart” “apparent” “appear” “apply” “appoint”
[36] “approach” “arm” “around” “art” “as”
[41] “ask” “at” “attend” “authority” “away”
[46] “awful” “each” “early” “east” “easy”
[51] “eat” “economy” “effect” “egg” “eight”
[56] “either” “elect” “electric” “eleven” “employ”
[61] “end” “english” “enjoy” “enough” “enter”
[66] “environment” “equal” “especial” “even” “evening”
[71] “ever” “every” “exact” “except” “exist”
[76] “expect” “explain” “express” “identify” “if”
[81] “important” “in” “indeed” “individual” “industry”
[86] “inform” “instead” “interest” “invest” “it”
[91] “item” “obvious” “occasion” “odd” “of”
[96] “off” “offer” “often” “okay” “old”
[101] “on” “only” “open” “opportunity” “or”
[106] “order” “original” “other” “ought” “out”
[111] “over” “own” “under” “understand” “union”
[116] “unit” “university” “unless” “until” “up”
[121] “upon” “usual”

Are there any words that contain at least one of each different vowel?

character(0)

NO

14.1.1 Q2 What word has the highest number of vowels?

[1] 5

What word has the highest proportion of vowels?

A tibble: 1 x 4

words count length proportion 1 a 1 1 1

14.4.2 Q1 In the previous example, you might have noticed that the regular expression matched “flickered”, which is not a colour. Modify the regex to fix the problem.

[1] “\b(red|orange|yellow|green|blue|purple)\b”

[1] “\b(red|orange|yellow|green|blue|purple)\b”

[1] “Glue the sheet to the dark blue background.”
[2] “Two blue fish swam in the tank.”
[3] “A wisp of cloud hung in the blue air.”
[4] “Leaves turn brown and yellow in the fall.”
[5] “The spot on the blotter was made by green ink.” [6] “The sofa cushion is red and of light weight.”
[7] “The sky that morning was clear and bright blue.” [8] “A blue crane is a tall wading bird.”
[9] “It is hard to erase blue or red ink.”
[10] “The lamp shone with a steady green flame.”

[1] “blue” “blue” “blue” “yellow” “green” “red” “blue”
[8] “blue” “blue” “green” “red” “red” “red” “green” [15] “green” “purple” “green” “red” “red” “blue” “blue”
[22] “red” “green” “green” “green” “yellow” “orange” “red”
[29] “red”

[1] “It is hard to erase blue or red ink.”
[2] “The sky in the west is tinged with orange red.”

14.4.2 Q2 From the Harvard sentences data, extract: The first word from each sentence.

[1] “The” “Glue” “It’s” “These” “Rice” “The” “The” “The”
[9] “Four” “Large”

All words ending in ing.

[1] “spring.” “evening.” “morning.” “winding” “living.”
[6] “king” “Adding” “making” “raging” “playing”
[11] “sleeping” “ring.” “glaring” “sinking.” “dying”
[16] “Bring” “lodging” “filing” “making” “morning”
[21] “wearing” “Bring” “wading” “swing” “nothing.”
[26] “ring” “morning” “sing” “sleeping” “painting.” [31] “walking” “bring” “bring” “shipping.” “spring”
[36] “ring” “winding” “puzzling” “spring” “landing.”
[41] “thing” “waiting” “whistling” “nothing.” “timing”
[46] “thing” “spring” “changing.” “drenching” “moving”
[51] “working” “ring”

All plurals.

[1] “planks” “days” “bowls” “lemons” “hogs”
[6] “hours” “stockings” “helps” “fires” “across”
[11] “bonds” “Press” “useless” “kittens” “days”
[16] “Sickness” “grass” “books” “keeps” “leads”

14.4.3 Q1 Find all words that come after a “number” like “one”, “two”, “three” etc. Pull out both the number and the word.

[1] “\b(one|two|three|four|five|six|seven|eight|nine|ten)\b ([^ ]+)”

[1] “seven books” “two met” “two factors” “three lists”
[5] “seven is” “two when” “ten inches.” “one war”
[9] “one button” “six minutes.” “ten years” “two shares”
[13] “two distinct” “five cents” “two pins” “five robins.” [17] “four kinds” “three story” “three inches” “six comes”
[21] “three batches” “two leaves.”

14.4.3 Q2 Find all contractions. Separate out the pieces before and after the apostrophe.

[[1]] [1] “It” “s”

[[2]] [1] “man” “s”

[[3]] [1] “don” “t”

[[4]] [1] “store” “s”

[[5]] [1] “workmen” “s”

[[6]] [1] “Let” “s”

[[7]] [1] “sun” “s”

[[8]] [1] “child” “s”

[[9]] [1] “king” “s”

[[10]] [1] “It” “s”

[[11]] [1] “don” “t”

[[12]] [1] “queen” “s”

[[13]] [1] “don” “t”

[[14]] [1] “pirate” “s”

[[15]] [1] “neighbor” “s”

14.4.4 Q1 Replace all forward slashes in a string with backslashes.

Hello/Hello/Hello

Hello

14.4.4 Q2 Implement a simple version of str_to_lower() using replace_all().

[1] “this is a test”

[1] “this is a test”

14.4.4 Q3 Switch the first and last letters in words.

[1] “a” “ebla” “tboua” “ebsoluta” “tccepa”
[6] “tccouna” “echieva” “scrosa” “tca” “ectiva”
[11] “lctuaa” “dda” “sddresa” “tdmia” “edvertisa” [16] “tffeca” “dffora” “rftea” “nfternooa” “ngaia”

Which of those strings are still words? those with the same first and last letters will return the same words. For example:

[1] “a” “america” “area” “dad” “dead”
[6] “depend” “educate” “else” “encourage” “engine”
[11] “europe” “evidence” “example” “excuse” “exercise”
[16] “expense” “experience” “eye” “health” “high”
[21] “knock” “level” “local” “nation” “non”
[26] “rather” “refer” “remember” “serious” “stairs”
[31] “test” “tonight” “transport” “treat” “trust”
[36] “window” “yesterday”

14.4.5 Q1 Split up a string like “apples, pears, and bananas” into individual components.

[[1]] [1] “apples” “pears” “and” “bananas”

Why is it better to split up by boundary(“word”) than " "?

[[1]] [1] “apples,” “pears,” “and” “bananas”

Because we dont want “apple,” we want “apple”

14.4.5 Q3 What does splitting with an empty string ("") do? Experiment, and then read the documentation.

[[1]] [1] “a” “p” “p” “l” “e” “s” “,” " " “p” “e” “a” “r” “s” “,” " " “a” “n” [18] “d” " " “b” “a” “n” “a” “n” “a” “s” This function split the strings into characters

14.5.1 Q1 How would you find all strings containing  with regex() vs. with fixed()?

[1] “contains \”

[1] “contains \”

14.4.5 Q2 What are the five most common words in sentences?

A tibble: 5 x 2

words n 1 the 751 2 a 202 3 of 132 4 to 123 5 and 118 Find the stringi functions that: Count the number of words.

[[1]] [1] “Count” “the” “number” “of” “words”

[1] 5

Find duplicated strings.

[1] FALSE FALSE TRUE FALSE

Generate random text.

[1] “gNvyF” “O7FZw” “V1CzG” “stoXi” “nvbuo” “Qp4M6” “Cf7B6” “uEOSl” [9] “Up4hf” “nDVaU” 14.7.1 Q2 How do you control the language that stri_sort() uses for sorting?

[1] “A” “B” “R”

[1] “R” “B” “A”

[1] “A” “B” “C” “D” “E” “F” “G” “H” “I” “J” “K” “L” “M” “N” “O” “P” “Q” [18] “R” “S” “T” “U” “V” “W” “X” “Y” “Z”

[1] “Z” “Y” “X” “W” “V” “U” “T” “S” “R” “Q” “P” “O” “N” “M” “L” “K” “J” [18] “I” “H” “G” “F” “E” “D” “C” “B” “A”