df <- read.csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/college-majors/majors-list.csv")

Q1. Using the 173 majors listed in fivethirtyeight.com’s College Majors dataset, provide code that identifies the majors that contain either “DATA” or “STATISTICS”

selected_majors <- df[grep("DATA|STATISTICS", df$Major, ignore.case = TRUE), ]

print(selected_majors[c("FOD1P", "Major", "Major_Category")])
##    FOD1P                                         Major          Major_Category
## 44  6212 MANAGEMENT INFORMATION SYSTEMS AND STATISTICS                Business
## 52  2101      COMPUTER PROGRAMMING AND DATA PROCESSING Computers & Mathematics
## 59  3702               STATISTICS AND DECISION SCIENCE Computers & Mathematics

Q2. The code that transforms the data listed in Q2

fruits <- c("bell pepper", "bilberry", "blackberry", "blood orange", 
            "blueberry", "cantaloupe", "chili pepper", "cloudberry", 
            "elderberry", "lime", "lychee", "mulberry", 
            "olive", "salal berry")
print(fruits)
##  [1] "bell pepper"  "bilberry"     "blackberry"   "blood orange" "blueberry"   
##  [6] "cantaloupe"   "chili pepper" "cloudberry"   "elderberry"   "lime"        
## [11] "lychee"       "mulberry"     "olive"        "salal berry"

Q3. Describe, in words, what these expressions will match:

(.)\1\1: This matches any character (.) followed by itself twice. So any three identical characters in a row, like “aaa” or “111”.

“(.)(.)\2\1”: This matches any two characters where the first character is followed by a different second, and then those two are repeated in reverse order. This would match strings.

(..)\1: This matches any sequence of two characters followed by the exact same sequence.

“(.).\1.\1”: matches a pattern where a character (.) is followed by any character (which is not captured), followed by the first captured character again, another non-captured character, and then the first captured character again. It matches a string.

“(.)(.)(.).\3\2\1”: This matches a pattern of any three characters followed by any number of characters (including none, because of .), and then those first three characters in reverse order. This also captures strings.

Q4. To construct regular expressions to match words that: