library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.0     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.1.8
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors

1. 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”

Allages = read.csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/college-majors/all-ages.csv", stringsAsFactors = F)

str (Allages)
## 'data.frame':    173 obs. of  11 variables:
##  $ Major_code                   : int  1100 1101 1102 1103 1104 1105 1106 1199 1301 1302 ...
##  $ Major                        : chr  "GENERAL AGRICULTURE" "AGRICULTURE PRODUCTION AND MANAGEMENT" "AGRICULTURAL ECONOMICS" "ANIMAL SCIENCES" ...
##  $ Major_category               : chr  "Agriculture & Natural Resources" "Agriculture & Natural Resources" "Agriculture & Natural Resources" "Agriculture & Natural Resources" ...
##  $ Total                        : int  128148 95326 33955 103549 24280 79409 6586 8549 106106 69447 ...
##  $ Employed                     : int  90245 76865 26321 81177 17281 63043 4926 6392 87602 48228 ...
##  $ Employed_full_time_year_round: int  74078 64240 22810 64937 12722 51077 4042 5074 65238 39613 ...
##  $ Unemployed                   : int  2423 2266 821 3619 894 2070 264 261 4736 2144 ...
##  $ Unemployment_rate            : num  0.0261 0.0286 0.0302 0.0427 0.0492 ...
##  $ Median                       : int  50000 54000 63000 46000 62000 50000 63000 52000 52000 58000 ...
##  $ P25th                        : int  34000 36000 40000 30000 38500 35000 39400 35000 38000 40500 ...
##  $ P75th                        : num  80000 80000 98000 72000 90000 75000 88000 75000 75000 80000 ...

Every major that includes the words “DATA” or “STATISTICS” must be searched for.

str_subset(Allages$Major, "DATA|STATISTICS", negate = F)
## [1] "COMPUTER PROGRAMMING AND DATA PROCESSING"     
## [2] "STATISTICS AND DECISION SCIENCE"              
## [3] "MANAGEMENT INFORMATION SYSTEMS AND STATISTICS"

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”)

Fruits <- c("bell pepper", "bilberry", "blackberry", "blood orange", "blueberry", "cantaloupe", "chili pepper", "cloudberry", "elderberry", "lime", "lychee", "mulberry", "olive", "salal berry")
x<- paste('c(', paste('"',Fruits,'"',sep = "", collapse = ','), sep = "",')')
writeLines(x)
## c("bell pepper","bilberry","blackberry","blood orange","blueberry","cantaloupe","chili pepper","cloudberry","elderberry","lime","lychee","mulberry","olive","salal berry")

3 Describe, in words, what these expressions will match:

(.)\1\1 “(.)(.)\2\1” (..)\1 “(.).\1.\1” “(.)(.)(.).*\3\2\1”

“(.)\1\1” It will search for the first letter at the beginning and check to see if it appears twice more after that.

“(.)(.)\2\1” I’ll examine whether anything matches the opposite by examining the first two starting letters.

“(..)\1” It will look at the first two letters to check for repetitions.

“(.).\1.\1” The initial letter—which does not begin on a new line—will be examined first, followed by a character, the first character once more, a character after that, and finally the first character once more.

4 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.)

str_subset(c("civic", "level", "mom", "nose", "eye"), "^(.)((.*\\1$)|\\1?$)")
## [1] "civic" "level" "mom"   "eye"
str_subset(c("foot", "good", "house", "letter", "data", "church"), "([A-Za-z][A-Za-z]).*\\1")
## [1] "church"
str_subset("eleven",  "([a-z]).*\\1.*\\1")
## [1] "eleven"