Exercise 1.

Question: Using the 173 majors listed in fivethirtyeight.com’s College Majors dataset [https://fivethirtyeight.com/features/the-economic-guide-to-picking-a-college-major/], provide code that identifies the majors that contain either “DATA” or “STATISTICS”

Answer: First, I’ll get the data from github, and create a data frame from the CSV of major names:

x <- getURL("https://raw.github.com/fivethirtyeight/data/master/college-majors/majors-list.csv",.opts=curlOptions(followlocation = TRUE)) 

majors <- read.csv(text = x, header=FALSE)

Then I’ll create a vector of major names and identify those that match “DATA” or “STATISTICS”:

major_names <- majors$V2

majors %>%
  filter(str_detect(major_names, "STATISTICS|DATA"))

Exercise 2.

Write code that transforms the data below:

[1] “bell pepper” “bilberry” “blackberry” “blood orange”

[5] “blueberry” “cantaloupe” “chili pepper” “cloudberry”

[9] “elderberry” “lime” “lychee” “mulberry”

[13] “olive” “salal berry”

Into a format like this:

c(“bell pepper”, “bilberry”, “blackberry”, “blood orange”, “blueberry”, “cantaloupe”, “chili pepper”, “cloudberry”, “elderberry”, “lime”, “lychee”, “mulberry”, “olive”, “salal berry”)

fruit_vector <- '[1] "bell pepper"  "bilberry"     "blackberry"   "blood orange"

[5] "blueberry"    "cantaloupe"   "chili pepper" "cloudberry"  

[9] "elderberry"   "lime"         "lychee"       "mulberry"    

[13] "olive"        "salal berry"'

fruit_vector <- str_extract_all(fruit_vector, pattern = "([a-z]+.[a-z]+)") 
fruit_vector <- unlist(fruit_vector) #found on stack overflow, to turn from list back into a vector

# I wasn't sure if the goal was to have a string output, so took a crack at it here: 
final_string <- paste('c("',paste(fruit_vector,collapse = '","'),'")',collapse = '')
final_string
## [1] "c(\" bell pepper\",\"bilberry\",\"blackberry\",\"blood orange\",\"blueberry\",\"cantaloupe\",\"chili pepper\",\"cloudberry\",\"elderberry\",\"lime\",\"lychee\",\"mulberry\",\"olive\",\"salal berry \")"

Exercise 3:

Leveraged these Regex resources: - https://rstudio.com/wp-content/uploads/2016/09/RegExCheatsheet.pdf - https://www.regular-expressions.info/backref.html

Question: Describe, in words, what these expressions will match: (.)\1\1 “(.)(.)\2\1” (..)\1 “(.).\1.\1” "(.)(.)(.).*\3\2\1"

Answer:

Exercise 4

Question: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.)

Answer: * Start and end with the same character: "^(.).*\1$"

str_subset(words, pattern = "^(.).*\\1$") 
##  [1] "america"    "area"       "dad"        "dead"       "depend"    
##  [6] "educate"    "else"       "encourage"  "engine"     "europe"    
## [11] "evidence"   "example"    "excuse"     "exercise"   "expense"   
## [16] "experience" "eye"        "health"     "high"       "knock"     
## [21] "level"      "local"      "nation"     "non"        "rather"    
## [26] "refer"      "remember"   "serious"    "stairs"     "test"      
## [31] "tonight"    "transport"  "treat"      "trust"      "window"    
## [36] "yesterday"
str_subset(words, pattern = "([a-z][a-z]).*\\1") 
##  [1] "appropriate" "church"      "condition"   "decide"      "environment"
##  [6] "london"      "paragraph"   "particular"  "photograph"  "prepare"    
## [11] "pressure"    "remember"    "represent"   "require"     "sense"      
## [16] "therefore"   "understand"  "whether"
str_subset(words, pattern = "([a-z]).*\\1.*\\1")
##  [1] "appropriate" "available"   "believe"     "between"     "business"   
##  [6] "degree"      "difference"  "discuss"     "eleven"      "environment"
## [11] "evidence"    "exercise"    "expense"     "experience"  "individual" 
## [16] "paragraph"   "receive"     "remember"    "represent"   "telephone"  
## [21] "therefore"   "tomorrow"