library(tidyverse)
PA 5.2: Scrambled Message
Regular Expressions
Pick your poison!
You may choose to work through Q1 - Q6 using a data.frame message_data
and follow the dplyr
pipeline syntax.
<- read_csv("https://github.com/earobinson95/stat331-calpoly/raw/master/practice-activities/data/scrambled_message.txt")
message_data
class(message_data)
[1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
or you might prefer to work with the character vector message
and use indexing – e.g. message[1] gives you the first element.
<- message_data |>
message pull(Word)
class(message)
[1] "character"
In this activity, a “word” is a set of characters with no white space. That is, even though many of elements of the scrambled mess vector are nonsense, and some have punctuation, you can consider each element to be a “word”.
You may find the R Regex Tester useful in this activity.
Warm-up exercises
How many characters are in the scrambled message?
How many words are in the scrambled message?
Print out every piece of a word in the scrambled message that starts with the letter “m”.
Print out every piece of a word in the scrambled message that ends with the letter “z”
Print out every punctuation symbol in the scrambled message.
Print out the longest word in the scrambled message.
Decode a message
You likely want to work with the message
character vector for decoding.
Complete the following steps to decode the message.
Remove any spaces before or after each word.
No word should be longer than 16 characters. Drop all extra characters off the end of each word.
Any time you see the word “ugh”, with any number of h’s, followed by a punctuation mark, delete this.
Replace all instances of exactly 2 a’s with exactly 2 e’s.
Replace all z’s with t’s.
Every word that ends in b, change that to a y.
Every word that starts with k, change that to a v.
Use
str_c()
to recombine all your words into a message.Find the movie this quote is from.
What is the name of the movie the quote is from?