So how should you complete your homework for this class?
Upload one file to Blackboard.
Homework 10:
Read: Chapter 14
Exercises:
Do 14.2.5 Exercises 1, 3
Do 14.3.2.1 Exercise 2 (This problem should not be turned in bacause of the str_view() makes it
so your notebook will not knit.)
Do 14.4.6.1 Exercise 1
library(pacman)
p_load("tidyverse", "stringr")
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?
Answer:
The paste() function includes a space at the end of the paste and paste0 does not have a space added at the end.
paste("first", "second")
[1] "first second"
paste0("first", "second")
[1] "firstsecond"
The stringr function that could be used in place of paste0() is str_c()
str_c("first", "second")
[1] "firstsecond"
The str_c() returns an NA if any of the strings are NA. This is different from the paste()
paste("first", NA)
[1] "first NA"
str_c("first", NA)
[1] NA
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?
Answer:
len <- str_length("firost")
# If len is even
str_sub("first", ceiling(len/2), ceiling(len/2))
[1] "r"
# If len is odd
str_sub("firost", ceiling(len/2), ceiling(len/2)+1)
[1] "ro"
# The mod function computes the remainder of a division problem. If the reminder is 0 then the length is even.
middle_letter <- function(x){
if (str_length(x) %% 2 == 0) {
middle_letter = str_sub(x, ceiling(len/2), ceiling(len/2)+1)
} else {
middle_letter = str_sub(x, ceiling(len/2), ceiling(len/2))
}
return(middle_letter)
}
x <- c("first")
middle_letter(x)
[1] "r"
Given the corpus of common words in stringr::words, create regular expressions that find all words that:
Answer:
words
[1] "a" "able" "about" "absolute" "accept" "account" "achieve"
[8] "across" "act" "active" "actual" "add" "address" "admit"
[15] "advertise" "affect" "afford" "after" "afternoon" "again" "against"
[22] "age" "agent" "ago" "agree" "air" "all" "allow"
[29] "almost" "along" "already" "alright" "also" "although" "always"
[36] "america" "amount" "and" "another" "answer" "any" "apart"
[43] "apparent" "appear" "apply" "appoint" "approach" "appropriate" "area"
[50] "argue" "arm" "around" "arrange" "art" "as" "ask"
[57] "associate" "assume" "at" "attend" "authority" "available" "aware"
[64] "away" "awful" "baby" "back" "bad" "bag" "balance"
[71] "ball" "bank" "bar" "base" "basis" "be" "bear"
[78] "beat" "beauty" "because" "become" "bed" "before" "begin"
[85] "behind" "believe" "benefit" "best" "bet" "between" "big"
[92] "bill" "birth" "bit" "black" "bloke" "blood" "blow"
[99] "blue" "board" "boat" "body" "book" "both" "bother"
[106] "bottle" "bottom" "box" "boy" "break" "brief" "brilliant"
[113] "bring" "britain" "brother" "budget" "build" "bus" "business"
[120] "busy" "but" "buy" "by" "cake" "call" "can"
[127] "car" "card" "care" "carry" "case" "cat" "catch"
[134] "cause" "cent" "centre" "certain" "chair" "chairman" "chance"
[141] "change" "chap" "character" "charge" "cheap" "check" "child"
[148] "choice" "choose" "Christ" "Christmas" "church" "city" "claim"
[155] "class" "clean" "clear" "client" "clock" "close" "closes"
[162] "clothe" "club" "coffee" "cold" "colleague" "collect" "college"
[169] "colour" "come" "comment" "commit" "committee" "common" "community"
[176] "company" "compare" "complete" "compute" "concern" "condition" "confer"
[183] "consider" "consult" "contact" "continue" "contract" "control" "converse"
[190] "cook" "copy" "corner" "correct" "cost" "could" "council"
[197] "count" "country" "county" "couple" "course" "court" "cover"
[204] "create" "cross" "cup" "current" "cut" "dad" "danger"
[211] "date" "day" "dead" "deal" "dear" "debate" "decide"
[218] "decision" "deep" "definite" "degree" "department" "depend" "describe"
[225] "design" "detail" "develop" "die" "difference" "difficult" "dinner"
[232] "direct" "discuss" "district" "divide" "do" "doctor" "document"
[239] "dog" "door" "double" "doubt" "down" "draw" "dress"
[246] "drink" "drive" "drop" "dry" "due" "during" "each"
[253] "early" "east" "easy" "eat" "economy" "educate" "effect"
[260] "egg" "eight" "either" "elect" "electric" "eleven" "else"
[267] "employ" "encourage" "end" "engine" "english" "enjoy" "enough"
[274] "enter" "environment" "equal" "especial" "europe" "even" "evening"
[281] "ever" "every" "evidence" "exact" "example" "except" "excuse"
[288] "exercise" "exist" "expect" "expense" "experience" "explain" "express"
[295] "extra" "eye" "face" "fact" "fair" "fall" "family"
[302] "far" "farm" "fast" "father" "favour" "feed" "feel"
[309] "few" "field" "fight" "figure" "file" "fill" "film"
[316] "final" "finance" "find" "fine" "finish" "fire" "first"
[323] "fish" "fit" "five" "flat" "floor" "fly" "follow"
[330] "food" "foot" "for" "force" "forget" "form" "fortune"
[337] "forward" "four" "france" "free" "friday" "friend" "from"
[344] "front" "full" "fun" "function" "fund" "further" "future"
[351] "game" "garden" "gas" "general" "germany" "get" "girl"
[358] "give" "glass" "go" "god" "good" "goodbye" "govern"
[365] "grand" "grant" "great" "green" "ground" "group" "grow"
[372] "guess" "guy" "hair" "half" "hall" "hand" "hang"
[379] "happen" "happy" "hard" "hate" "have" "he" "head"
[386] "health" "hear" "heart" "heat" "heavy" "hell" "help"
[393] "here" "high" "history" "hit" "hold" "holiday" "home"
[400] "honest" "hope" "horse" "hospital" "hot" "hour" "house"
[407] "how" "however" "hullo" "hundred" "husband" "idea" "identify"
[414] "if" "imagine" "important" "improve" "in" "include" "income"
[421] "increase" "indeed" "individual" "industry" "inform" "inside" "instead"
[428] "insure" "interest" "into" "introduce" "invest" "involve" "issue"
[435] "it" "item" "jesus" "job" "join" "judge" "jump"
[442] "just" "keep" "key" "kid" "kill" "kind" "king"
[449] "kitchen" "knock" "know" "labour" "lad" "lady" "land"
[456] "language" "large" "last" "late" "laugh" "law" "lay"
[463] "lead" "learn" "leave" "left" "leg" "less" "let"
[470] "letter" "level" "lie" "life" "light" "like" "likely"
[477] "limit" "line" "link" "list" "listen" "little" "live"
[484] "load" "local" "lock" "london" "long" "look" "lord"
[491] "lose" "lot" "love" "low" "luck" "lunch" "machine"
[498] "main" "major" "make" "man" "manage" "many" "mark"
[505] "market" "marry" "match" "matter" "may" "maybe" "mean"
[512] "meaning" "measure" "meet" "member" "mention" "middle" "might"
[519] "mile" "milk" "million" "mind" "minister" "minus" "minute"
[526] "miss" "mister" "moment" "monday" "money" "month" "more"
[533] "morning" "most" "mother" "motion" "move" "mrs" "much"
[540] "music" "must" "name" "nation" "nature" "near" "necessary"
[547] "need" "never" "new" "news" "next" "nice" "night"
[554] "nine" "no" "non" "none" "normal" "north" "not"
[561] "note" "notice" "now" "number" "obvious" "occasion" "odd"
[568] "of" "off" "offer" "office" "often" "okay" "old"
[575] "on" "once" "one" "only" "open" "operate" "opportunity"
[582] "oppose" "or" "order" "organize" "original" "other" "otherwise"
[589] "ought" "out" "over" "own" "pack" "page" "paint"
[596] "pair" "paper" "paragraph" "pardon" "parent" "park" "part"
[603] "particular" "party" "pass" "past" "pay" "pence" "pension"
[610] "people" "per" "percent" "perfect" "perhaps" "period" "person"
[617] "photograph" "pick" "picture" "piece" "place" "plan" "play"
[624] "please" "plus" "point" "police" "policy" "politic" "poor"
[631] "position" "positive" "possible" "post" "pound" "power" "practise"
[638] "prepare" "present" "press" "pressure" "presume" "pretty" "previous"
[645] "price" "print" "private" "probable" "problem" "proceed" "process"
[652] "produce" "product" "programme" "project" "proper" "propose" "protect"
[659] "provide" "public" "pull" "purpose" "push" "put" "quality"
[666] "quarter" "question" "quick" "quid" "quiet" "quite" "radio"
[673] "rail" "raise" "range" "rate" "rather" "read" "ready"
[680] "real" "realise" "really" "reason" "receive" "recent" "reckon"
[687] "recognize" "recommend" "record" "red" "reduce" "refer" "regard"
[694] "region" "relation" "remember" "report" "represent" "require" "research"
[701] "resource" "respect" "responsible" "rest" "result" "return" "rid"
[708] "right" "ring" "rise" "road" "role" "roll" "room"
[715] "round" "rule" "run" "safe" "sale" "same" "saturday"
[722] "save" "say" "scheme" "school" "science" "score" "scotland"
[729] "seat" "second" "secretary" "section" "secure" "see" "seem"
[736] "self" "sell" "send" "sense" "separate" "serious" "serve"
[743] "service" "set" "settle" "seven" "sex" "shall" "share"
[750] "she" "sheet" "shoe" "shoot" "shop" "short" "should"
[757] "show" "shut" "sick" "side" "sign" "similar" "simple"
[764] "since" "sing" "single" "sir" "sister" "sit" "site"
[771] "situate" "six" "size" "sleep" "slight" "slow" "small"
[778] "smoke" "so" "social" "society" "some" "son" "soon"
[785] "sorry" "sort" "sound" "south" "space" "speak" "special"
[792] "specific" "speed" "spell" "spend" "square" "staff" "stage"
[799] "stairs" "stand" "standard" "start" "state" "station" "stay"
[806] "step" "stick" "still" "stop" "story" "straight" "strategy"
[813] "street" "strike" "strong" "structure" "student" "study" "stuff"
[820] "stupid" "subject" "succeed" "such" "sudden" "suggest" "suit"
[827] "summer" "sun" "sunday" "supply" "support" "suppose" "sure"
[834] "surprise" "switch" "system" "table" "take" "talk" "tape"
[841] "tax" "tea" "teach" "team" "telephone" "television" "tell"
[848] "ten" "tend" "term" "terrible" "test" "than" "thank"
[855] "the" "then" "there" "therefore" "they" "thing" "think"
[862] "thirteen" "thirty" "this" "thou" "though" "thousand" "three"
[869] "through" "throw" "thursday" "tie" "time" "to" "today"
[876] "together" "tomorrow" "tonight" "too" "top" "total" "touch"
[883] "toward" "town" "trade" "traffic" "train" "transport" "travel"
[890] "treat" "tree" "trouble" "true" "trust" "try" "tuesday"
[897] "turn" "twelve" "twenty" "two" "type" "under" "understand"
[904] "union" "unit" "unite" "university" "unless" "until" "up"
[911] "upon" "use" "usual" "value" "various" "very" "video"
[918] "view" "village" "visit" "vote" "wage" "wait" "walk"
[925] "wall" "want" "war" "warm" "wash" "waste" "watch"
[932] "water" "way" "we" "wear" "wednesday" "wee" "week"
[939] "weigh" "welcome" "well" "west" "what" "when" "where"
[946] "whether" "which" "while" "white" "who" "whole" "why"
[953] "wide" "wife" "will" "win" "wind" "window" "wish"
[960] "with" "within" "without" "woman" "wonder" "wood" "word"
[967] "work" "world" "worry" "worse" "worth" "would" "write"
[974] "wrong" "year" "yes" "yesterday" "yet" "you" "young"
str_view(words, "^y", match = TRUE)
str_view(words, "x$", match = TRUE)
# The way we are not supposed to do this.
words_three <- as.data.frame(words) %>% mutate(words_length = str_length(words) ) %>%
filter(words_length == 3)
words_three
dim(words_three)
[1] 110 2
words_three2 <- str_view(words, "^...$", match = TRUE)
words_three2
str_view(words, ".......", match = TRUE)
Split up a string like “apples, pears, and bananas” into individual components.
Answer:
I think the best answer to this question uses the helper function boundary(“word”).
If you gave “, +(and +)?” in place of boundary(“word”) you should be sure you understand what it is doing.
x <- c("apples, pears, and bananas")
str_split(x, ", ")[[1]]
[1] "apples" "pears" "and bananas"
str_split(x, boundary("word"))[[1]]
[1] "apples" "pears" "and" "bananas"