[1] "banana"
7 Adv Data wrangling II
🧩 Learning Goals
By the end of this lesson, you should be able to:
- Manipulate and explore strings using the
stringrpackage - Construct regular expressions to find patterns in strings
Helpful Cheatsheets
The stringr cheatsheet (HTML, PDF) will be useful to have open and reference.
Motivation: 30 Years of American Anxieties
In 2018 the data journalism organization The Pudding featured a story called 30 Years of American Anxieties about themes in 30 years of posts to the Dear Abby column (an American advice column).
One way to understand themes in text data is to conduct a qualitative analysis, a methodology in which multiple readers read through instances of text several times to reach a consensus about themes.
Another way to understand themes in text data is computational text analysis.
- This is what we will explore today.
Both qualitative analysis and computational tools can be used in tandem. Often, using computational tools can help focus a close reading of select texts, which parallels the spirit of a qualitative analysis.
To prepare ourselves for a computational analysis, let’s learn about strings.
Strings
Strings are objects of the character class (abbreviated as <chr> in tibbles).
When you print out strings, they display with double quotes:
Working with strings generally will involve the use of regular expressions, a tool for finding patterns in strings.
Regular expressions (regex, for short) look like the following:
"^the" (Strings that start with "the")
"end$" (Strings that end with "end")
Before getting to regular expressions, let’s go over some fundamentals about working with strings. The stringr package (available within tidyverse) is great for working with strings.
Creating strings
Creating strings by hand is useful for testing out regular expressions.
To create a string, type any text in either double quotes (") or single quotes '. Using double or single quotes doesn’t matter unless your string itself has single or double quotes.
Code
[1] "character"
[1] "character"
[1] "character"
[1] 1
[1] 1
[1] 2
We can view these strings “naturally” (without the opening and closing quotes) with str_view():
[1] │ This is a string
[1] │ If I want to include a "quote" inside a string, I use single quotes
[1] │ This is a string
[2] │ If I want to include a "quote" inside a string, I use single quotes
Exercise: Create the string It's Thursday. What happens if you put the string inside single quotes? Double quotes?
Because " and ' are special characters in the creation of strings, R offers another way to put them inside a string. We can escape these special characters by putting a \ in front of them:
Code
[1] │ This is a string with "double quotes"
[1] │ This is a string with 'single quotes'
Given that \ is a special character, how can we put the \ character in strings? We have to escape it with \\.
Exercise: Create the string C:\Users. What happens when you don’t escape the \?
Other special characters include:
-
\t(Creates a tab) -
\n(Creates a newline)
Both can be useful in plots to more neatly arrange text.
[1] │ Record temp:{\t}102
[1] │ Record temp:
│ {\t}102
Can we get str_view() to show the tab instead of {\t}? We can use the html argument to have the string displayed as if on a webpage:
Often we will want to create new strings within data frames. We can use str_c() or str_glue(), both of which are vectorized functions (meaning they take vectors as inputs and provide vectors as outputs - can be used within mutate()):
- With
str_c()the strings to be combined are all separate arguments separated by commas. - With
str_glue()the desired string is written as a template with variable names inside curly braces{}.
Code
# A tibble: 4 × 2
first_name last_name
<chr> <chr>
1 Arya Stark
2 Olenna Tyrell
3 Tyrion Lannister
4 Melisandre <NA>
Code
# A tibble: 4 × 4
first_name last_name full_name1 full_name2
<chr> <chr> <chr> <glue>
1 Arya Stark Arya Stark Arya Stark
2 Olenna Tyrell Olenna Tyrell Olenna Tyrell
3 Tyrion Lannister Tyrion Lannister Tyrion Lannister
4 Melisandre <NA> <NA> Melisandre NA
Exercise: In the following data frame, create a full date string in month-day-year format using both str_c() and str_glue().
Extracting information from strings
The str_length() counts the number of characters in a string.
Code
# A tibble: 2 × 3
name comment comment_length
<chr> <chr> <int>
1 Alice The essay was well organized around the core message and… 78
2 Bob Good job! 9
The str_sub() function gets a substring of a string. The 2nd and 3rd arguments indicate the beginning and ending position to extract.
- Negative positions indicate the position from the end of the word. (e.g., -3 indicates “3rd letter from the end”)
- Specifying a position that goes beyond the word won’t result in an error.
str_sub()will just go as far as possible.
[1] "App" "Ban" "Pea"
[1] "ple" "ana" "ear"
[1] "pple" "anana" "ear"
[1] "a"
Exercise: Using str_sub(), create a new variable with only the middle letter of each word in the data frame below. (Challenge: How would you handle words with an even number of letters?)
Finding patterns in strings with regular expressions
Suppose that you’re exploring text data looking for places where people describe happiness. There are many ways to search. We could search for the word “happy” but that excludes “happiness” so we might search for “happi”.
Regular expressions (regex) are a powerful language for describing patterns within strings.
. . .
We can use str_view() with the pattern argument to see what parts of a string match the regex supplied in the pattern argument. (Matches are enclosed in <>.)
[6] │ bil<berry>
[7] │ black<berry>
[10] │ blue<berry>
[11] │ boysen<berry>
[19] │ cloud<berry>
[21] │ cran<berry>
[29] │ elder<berry>
[32] │ goji <berry>
[33] │ goose<berry>
[38] │ huckle<berry>
[50] │ mul<berry>
[70] │ rasp<berry>
[73] │ salal <berry>
[76] │ straw<berry>
Essentials of forming a regex
- Letters and numbers in a regex are matched exactly and are called literal characters.
- Most punctuation characters, like
.,+,*,[,], and?, have special meanings and are called metacharacters. -
Quantifiers come after a regex and control how many times a pattern can match:
-
?: match the preceding pattern 0 or 1 times -
+: match the preceding pattern at least once -
*: match the preceding pattern at least 0 times (any number of times)
-
. . .
Exercise: Before running the code below, predict what matches will be made. Run the code to check your guesses. Note that in all regex’s below the ?, +, * applies to the b only (not the a).
- We can match any of a set of characters with
[](called a character class), e.g.,[abcd]matches “a”, “b”, “c”, or “d”.- We can invert the match by starting with
^:[^abcd]matches anything except “a”, “b”, “c”, or “d”.
- We can invert the match by starting with
[284] │ <exa>ct
[285] │ <exa>mple
[288] │ <exe>rcise
[289] │ <exi>st
[836] │ <sys>tem
[901] │ <typ>e
Exercise Using the words data, find words that have two vowels in a row followed by an “m”.
- The alternation operator
|can be read just like the logical operator|(“OR”) to pick between one or more alternative patterns. e.g.,apple|bananasearches for “apple” or “banana”.
[1] │ <apple>
[13] │ canary <melon>
[20] │ coco<nut>
[52] │ <nut>
[62] │ pine<apple>
[72] │ rock <melon>
[80] │ water<melon>
Exercise: Using the fruit data, find fruits that have a repeated vowel (“aa”, “ee”, “ii”, “oo”, or “uu”.)
[1] "blood orange" "gooseberry" "lychee"
[4] "purple mangosteen"
- The
^operator indicates the beginning of a string, and the$operator indicates the end of a string. e.g.,^amatches strings that start with “a”, anda$matches words that end with “a”. - Parentheses group together parts of a regular expression that should be taken as a bundle. (Much like parentheses in arithmetic statements.)
- e.g.,
ab+is a little confusing. Does it match “ab” one or more times? Or does it match “a” first, then just “b” one or more times? (The latter, as we saw in an earlier example.) We can be very explicit and usea(b)+.
- e.g.,
Exercise: Using the words data, find (1) words that start with “y” and (2) words that don’t start with “y”.
Exploring stringr functions
Read in the “Dear Abby” data underlying The Pudding’s 30 Years of American Anxieties article.
Take a couple minutes to scroll through the 30 Years of American Anxieties article to get ideas for themes that you might want to search for using regular expressions.
The following are core stringr functions that use regular expressions:
-
str_view()- View the first occurrence in a string that matches the regex -
str_count()- Count the number of times a regex matches within a string -
str_detect()- Determine if (TRUE/FALSE) the regex is found within string -
str_subset()- Return subset of strings that match the regex -
str_extract(), str_extract_all()- Return portion of each string that matches the regex.str_extract()extracts the first instance of the match.str_extract_all()extracts all matches. -
str_replace(), str_replace_all()- Replace portion of string that matches the regex with something else.str_replace()replaces the first instance of the match.str_replace_all()replaces all instances of the match. -
str_remove(), str_remove_all()- Removes the portion of the string that matches the pattern. Equivalent tostr_replace(x, "THE REGEX PATTERN", "")
Exercise: Starting from str_count(), explore each of these functions by pulling up the function documentation page and reading through the arguments. Try out each function using the posts data.
Code
# A tibble: 10 × 2
question_only n
<chr> <int>
1 "media-wise, this is going to be a tough campaign for barbara bush. he… 29
2 "i was offended by your response to \"nameless, of course,\" who was u… 20
3 "this story has been entered on the data base in 2 parts. this is part… 18
4 "my mother had me when she was only 16. by the time i was 3, i also ha… 17
5 "for years now, my dad's health has slowly deteriorated. he has good d… 17
6 "dear readers: in my fidelity survey that brought over 210,000 respons… 16
7 "i have been married for 27 years. two years ago, my husband was laid … 16
8 "my husband's sister is always asking us to send his parents money. sh… 16
9 "until i was 13, my 8-year-old brother and i lived with our mom, but s… 16
10 "my husband's aunt, \"vivian,\" recently confided a family secret. she… 16
# A tibble: 2 × 2
has_match n
<lgl> <int>
1 FALSE 7654
2 TRUE 12380
[1] 1353
[1] "first one grandma says, \"marriage is the price a man pays for sex, and sex is the price a woman pays for marriage.\"\nthen another grandma says, \"men give love in order to get sex, and women give sex in order to get love--and marriage has nothing to do with it.\"\nmy lover and i cannot marry because we are both men. our relationship is not based on the barter system. we're together because we love each other, and sex is a natural expression of that love--not a demand or a payment.\nare heterosexuals missing something?\nglad i'm gay"
[2] "you've heard from the jacks and the chucks, and then you said, \"now let's hear from the johns.\"\nhaven't you heard? johnny can't read. johnny can't write. johnny can't seem to do anything.\nmaybe that's why they say, \"let george do it.\"\njohn can in oregon\nevery teen-ager should know the truth about drugs, sex and how to be happy. for abby's booklet, also available in spanish, send your name and address clearly printed with a check or money order for $2.50 (this includes postage) to: abby, teen booklet, p.o. box 38923, hollywood, calif. 90038."
[3] "i would like to congratulate that young father of two who decided to get a vasectomy. (he's so hard up his grandmother pays his rent and utilities.)\nhe is a real man in my book. i was advised by my doctor not to have any more children. we have three, and we're saving up for me to have an operation that will cost twice as much as a vasectomy for my husband. he won't even discuss it, and the doctor says not to push him. how about all the times i'm \"pushed\" to have sex when i'm afraid my birth control might not work?\ni'm getting to resent him and sex. i envy my girlfriends whose husbands have had vasectomies. they are real men!\nconstantly worried"
[4] "my best friend of almost 15 years was married for the third time about a year ago.\ntwo months ago, she asked me if i was interested in \"swapping\" husbands. i immediately said \"no\" because: (1) i think it's immoral; (2) i value my marriage; (3) i do not want to have sex with another man . . . especially with my best friend's husband. (she said it was her husband's idea.)\nsince then, my best friend has been cool to me, but she has been coming on to my husband . . . flirting, pressing against him, etc. also, her husband has been coming on to me. he told me he has been dreaming about me, and ever since he heard i refused to go to bed with him, it makes him want me all the more.\ni asked my husband what he thought about the swapping idea just to see what he'd say, and he said, \"i'll think about it.\" i was angry because he didn't say no right off the bat like i did.\ni still value my friendship with my best friend, and i feel sorry for her because she married that jerk.\nhow can we stay friends with this couple without agreeing to swap, which i think is what everybody wants, except me?\nno names, small town"
[5] "after 12 years of marriage, my husband and i realized that our sex life was practically dead, so we went to a marriage counselor.\nthe counselor (a woman) told us that a good way to put a little pep in our marriage would be to tell each other about our sexual fantasies. should i tell him about mine? he's not in any of them.\nwondering"
[6] "sex without strings. sex without guilt. sex without sexism. sex without old-fashioned legal entanglements or prerequisites or consequences. sex simply for the joy of sex.\nthat was the promise and the premise of the change in sexual mores that is loosely dubbed the sexual revolution.\nbut like a balloon mortgage or a no-fault divorce, it now appears that the advantages of the relaxation in sexual mores may have been disastrously oversold. now it's turning out that sex without constraints isn't the same as sex without consequences. there may well be a steep and lasting price to pay for what used to be called promiscuity. and as usual where sex is concerned, the big losers are women.\nwhat's driving the re-evaluation of sexual conduct is the new and devastating outbreak of sexually transmitted disease (std) whose effects can include sterility, cancer, chronic pain, problem pregnancies, abnormal children and even death. the growing prevalence of std may make a case for monogamy in the minds of many who are not convinced by traditional morality.\nthe term sexually transmitted disease now includes much more than the historically familiar syphilis and gonorrhea. now there is also genital herpes, which claimed half a million new victims last year and remains an incurable, lifelong affliction. and chlamydia, the std with the fastest-rising incidence, with 3 million to 4 million new cases last year. and venereal warts, afflicting a million new victims annually and linked to increased risk of cancer. and deadly aids, which almost doubled in incidence in 1984 and, it is feared, could become a hazard to heterosexuals as well as homosexuals.\nall together, physicians now know of at least 25 diseases that are spread through sexual contact. many are occurring in epidemic numbers in the united states. epidemiologists estimate that about 27,000 new cases of std occur daily and that eventually 25 percent of all americans between the ages of 15 and 55 will be infected. already, medical bills for treating std top $2 billion a year.\nin many cases, there is little physicians can do, no antibiotic to solve the problem as with gonorrhea and syphilis. the effects may not only last a lifetime but may be passed on, with deadly consequences, to victims' children.\na mother who has active genital herpes may infect her baby during the process of birth. more than half of these infected infants die. survivors are likely to be blind or have permanent brain damage. doctors can minimize the risks by scheduling a caesarean birth when a mother has visible herpes sores. but some damaged babies are born to women without obvious infections.\nchlamydia and venereal warts can also be transmitted to newborns by infected mothers with devastating results. and there are cases in which infants have acquired fatal aids from mothers.\nthe litany of problems caused by std goes on. both chlamydia and gonorrhea, for example, can cause pelvic inflammatory disease, which can fill the fallopian tubes with scar tissue, increase the risk of a dangerous ectopic pregnancy and may make it impossible to have children. chlamydia now ranks as a significant cause of infertility in women.\nit's ironic that the same generation of young adults who have such a commendable obsession with fitness and health are also producing an unprecedented epidemic of sexually transmitted disease. the problem can no longer be brushed off with a shrug about \"the love bug\" or finessed with one of those \"personal ads\" in which \"attractive, slim single with good job and herpes seeks same for meaningful relationship.\"\nwomen, in particular, who care about the health of their bodies--and the well-being of future offspring--need to look realistically at the risks that go with multiple sex partners in the 1980s or even with an unlucky chance encounter, however romantic.\nneither dear abby nor miss manners nor even the centers for disease control can tell a sexually active adult a tactful, foolproof way to make sure a prospective sex partner won't leave her a nasty and permanent souvenir of their relationship. std, in fact, is turning out to be a powerful, scientific reinforcement of traditional morality and--what else?--monogamy."
[7] "prime-time tv is about to do gays the way they've never been done before. without jokes. without excuses. without ridicule or regrets.\n\"consenting adult,\" at 8 p.m. monday on abc-ch. 7, is the powerful story of what happens to a nice, middle-class family after the college-age son tells his parents he is . . .he is . . .he is . . . (\"just say it!\" his mother says) . . . a homosexual. \"you've got to let me be what i am because i can't change,\" the gay son (barry tubb) says.\n\"won't change!\" says his father (martin sheen).\n\"i'm the same person i've always been only now i'm accepting the fact i prefer to have sex with men,\" the son counters.\n\"no!\" screams his mother (marlo thomas).\nand so it goes. he wants their acceptance; they want him \"cured,\" and the anguish and struggle that result will speak to millions, all brought to you in the very best of taste by the same concerned adults at abc who brought you nuclear fryface (\"the day after\") and father-daughter incest (\"something about amelia\").\n\"when i first tried to sell this project,\" says producer ray aghayan, \"no one would touch it. . . .\"\nthat was 1975. aghayan was trying to make a career change, from a big- name costume designer (\"applause,\" \"funny lady,\" \"lady sings the blues\") to film producer, and the first project he optioned was laura z. hobson's \"consenting adult,\" about the coming out of her own son and her painful journey to acceptance.\n\"she'd written 'gentleman's agreement,' and that was a great movie that won lots of awards; so i decided to read 'consenting adult,' aghayan says. \"i thought it was wonderful! the story was honest and simple without being sensational.\"\naghayan pitched it to some film studios, but they took a pass.\n\"historically,\" he says, \"films about homosexuals have never made money.\"\nthen he took it to tv--to all three networks, he says--but it was dead meat there, too. some tv execs saw it as too hard, too controversial. others told him it was too soft and not controversial enough. \"the climate just wasn't right,\" he says.\nnow, 10 years later, the fog is lifting, just a little.\n\"our notion of homosexuality has changed,\" aghayan says. \"there are new groups, new laws and a new awareness that it's not a disease, not an illness to be cured.\"\nand tv is seen as beginning to reflect those changes. recently, \"kate & allie\" pretended to be lovers to keep their apartment. on \"hotel,\" robert reed played a homosexual sportswriter whose wife discovers he is gay. nbc's new \"sara\" series also has a gay character. last year donna pescow played a lesbian on \"all my children,\" but that was less than three months' work and she hasn't returned. tony randall's \"love, sidney\" series on nbc a few seasons back was a veiled attempt to do a gay guy, but no one bought the doublespeak deception, and the writing stank, too.\nmeanwhile, one very funny sitcom the networks turned down because one of the \"brothers\" is gay turned out to be one of the tv success stories of 1984. after the networks took a pass, \"showtime\" took a chance, and the cable channel is so happy with the results, it has just committed to a record- breaking order of 50 new episodes.\nthen there's \"dynasty.\" it's touted as the first prime-time network show to allow a major character to be gay, but steven is so confused about who he is and what he wants, the closet he keeps coming out of is more like a revolving door. and what about his gay friend, luke? it's okay for abc to crash, kill, burn and maim dozens of tv victims every week, but when it comes to steven and luke getting physical, it's strictly a hands off affair.\n\"hill street blues,\" \"st. elsewhere\" and \"the love boat\" also have taken a fling lately with homosexual characters, but only in minor roles, and only in a minor key.\n\"consenting adult\" is major. it explores the subject without exploiting it. the only other major tv movies on the subject that have come close in the last 10 years, aghayan says, are \"that certain summer,\" with hal holbrook and martin sheen, and \"a question of love,\" with jane alexander and gena rowlands.\n\" 'that certain summer' was a leap ahead of anything else that had been done,\" aghayan says, \"but it still seemed to be a negative view of homosexuals. it ends up with holbrook's telling his son he was gay, and his being all weepy and upset and apologetic. i wanted to do something positive.\n\"to me 'consenting adult' isn't a story about homosexuality. it's a story about acceptance. and love. it's about how we can't change our children and make them over into extensions of ourselves. and it's not just about children, but friends, too. if you love someone, you accept them for who they are. you can't change them. it just doesn't work.\"\nwhat does work is facing the truth. but that takes time. in the novel it takes the mother 13 years. in the tv version marlo thomas goes from rejection to repulsion to reconciliation in about a year and a half. indeed, if there is one basic flaw in the film, it's that the mother's final, final acceptance of her son happens inside her head, off screen. thomas wishes more time had been given to the process, but overall, she is very pleased with the project.\n\"i beliehamily dares to tell the truth about himself--\"i am a homosexual\"--the whole family is shaken up.\n\"but the son's courage eventually gave the mother courage to stop telling her lies, to face up to the sexual problems in her marriage. the whole thing is we shouldn't try to 'cure' them. homosexuality is not a disease. we should just let people be.\"\n'consenting adult' to raise issues, but support is available\nseveral national gay and lesbian groups are commending, and recommending, \"consenting adult,\" at 8 p.m. monday on abc-ch. 7.\nin a mailing, the national gay task force has encouraged members to thank abc for airing it.\n\"it's a real coming out for tv,\" says a representative of the alliance for gay and lesbian artists (agla). the los angeles-based group calls the film \"an important tv event.\"\nin chicago these groups offer information and support on issues raised in \"consenting adult\":\n-- gay and lesbian horizons. a professional, peer hotline for young people concerned about their sexuality. call 929-help afte -- parents and friends of lesbians and gays (parent's flag). recommended by dear abby. the hotline number is 472-3079.\n-- coming out workshops. rick karlin and his mother offer a workshoheir parents. the two may be reached through the gay and lesbian horizons hotline, 929-help.\ncaption:\nphoto: barry tubb and marlo thomas: exploring without exploiting.\nillustration photo"
[8] "you said, \"sex therapy is in--faking it is out.\"\ni wholeheartedly agree; that's why i'm writing. i faked satisfaction for 25 years, then i went to a counselor who said, \"most women are happy just satisfying their husbands.\"\nnow what?\nnot happy enough"
[9] "i would like to put in my 2 cents about that 41-year-old woman who took a 15-year-old neighbor boy to bed. what's all the commotion about? all right, so a 15-year-old boy is a minor in the eyes of the law, but there are a lot of boys that age who are man enough to seek a sexual relationship.\nin my day, it wasn't unusual for a boy's father to pay a high-class prostitute to educate his son. it may not be the ideal solution, but it's preferable to having the kid talk some inexperienced 14-year-old girl into taking care of his normal sex urges.\nmichigan grandpa, age 65"
[10] "my father and i have a difference of opinion on a moral matter. he still believes \"good\" girls save their virginity for their husbands. he says he never would have married my mother if she had not been a virgin. (i guess he took her word for it.)\nof course, my father sees nothing wrong with young men getting some sexual experience before they marry. i wonder who he thinks these young men are getting their experience with?\ni am a sophomore in college, and i believe a woman is entitled to as much sexual freedom as a man. i can't understand how a grown man living in today's society can still believe in the outdated \"i don't want used goods\" theory.\nhow can i set my father straight? no amount of arguing on my part has worked.\nstill intact in maryland"
Done!
- Check the ICA Instructions for how to (a) push your code to GitHub and (b) update your portfolio website
