title: “Assignment 3”
author: “Joe_Garcia”
date: ‘2023-02-12’
output: html_document

1.A We are provided with the data set from fivethrityeight which gives us 173 majors. In order to filter down the number we first must import the libraries that we use. Noticably dplyr.

library(knitr)
library(plyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(RCurl)
## Loading required package: bitops
library(stringr)
library(tidyverse)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
## Registered S3 method overwritten by 'rvest':
##   method            from
##   read_xml.response xml2
## ── Attaching packages ────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.1     ✔ readr   1.3.1
## ✔ tibble  2.1.1     ✔ purrr   1.0.1
## ✔ tidyr   0.8.3     ✔ forcats 0.4.0
## ── Conflicts ───────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::arrange()   masks plyr::arrange()
## ✖ purrr::compact()   masks plyr::compact()
## ✖ tidyr::complete()  masks RCurl::complete()
## ✖ dplyr::count()     masks plyr::count()
## ✖ dplyr::failwith()  masks plyr::failwith()
## ✖ dplyr::filter()    masks stats::filter()
## ✖ dplyr::id()        masks plyr::id()
## ✖ dplyr::lag()       masks stats::lag()
## ✖ dplyr::mutate()    masks plyr::mutate()
## ✖ dplyr::rename()    masks plyr::rename()
## ✖ dplyr::summarise() masks plyr::summarise()
## ✖ dplyr::summarize() masks plyr::summarize()

1.B. We then use the getURL() method to grab the data from the gethub.

url <- getURL("https://raw.githubusercontent.com/fivethirtyeight/data/master/college-majors/majors-list.csv")
majors_data <-data.frame(read.csv(text= url))

1.C. Here we have the data as noted by the head() method that gives us the first few rows in the data set.

head(majors_data)
##   FOD1P                                 Major
## 1  1100                   GENERAL AGRICULTURE
## 2  1101 AGRICULTURE PRODUCTION AND MANAGEMENT
## 3  1102                AGRICULTURAL ECONOMICS
## 4  1103                       ANIMAL SCIENCES
## 5  1104                          FOOD SCIENCE
## 6  1105            PLANT SCIENCE AND AGRONOMY
##                    Major_Category
## 1 Agriculture & Natural Resources
## 2 Agriculture & Natural Resources
## 3 Agriculture & Natural Resources
## 4 Agriculture & Natural Resources
## 5 Agriculture & Natural Resources
## 6 Agriculture & Natural Resources

1.D. Then we use dplyr’s filter method to find all the phrases that contain ‘DATA’ and ‘STATISTICS’. The grepl function works well for this.

stats_data_df <- dplyr::filter(majors_data, grepl("DATA|STATISTICS", Major))
stats_data_df
##   FOD1P                                         Major
## 1  6212 MANAGEMENT INFORMATION SYSTEMS AND STATISTICS
## 2  2101      COMPUTER PROGRAMMING AND DATA PROCESSING
## 3  3702               STATISTICS AND DECISION SCIENCE
##            Major_Category
## 1                Business
## 2 Computers & Mathematics
## 3 Computers & Mathematics

1.E We take only the column with the Data and Statistics in the title, so that we have their numbers.

sd_numbers <- stats_data_df[1]
sd_numbers
##   FOD1P
## 1  6212
## 2  2101
## 3  3702

2.A. Where going to combine these words into a list.

s0 =  '[1] "bell pepper" "bilberry"     "blackberry"   "blood orange"
[5] "blueberry"    "cantaloupe"   "chili pepper" "cloudberry"
[9] "elderberry"   "lime"       "lychee"       "mulberry"    
[13] "olive"  "salal berry"'

2.B. We primarily use gsub to remove the characters. We use double backslash brackets to get rid of the brackets.

s1 <- gsub("\\[|\\]", "", s0)

2.C. Then we use the [[:digit]]+ to remove all the numbers from above.

s2 <- gsub("[[:digit:]]+", "", s1)

2.D. We remove a couple of newline statements.

s3 <- gsub("\n", "", s2)

2.E. The str_squish() helps us remove the white spaces, leaving one white space between words.

#s4 <- str_remove_all(s3, "\\s{2,6}(?! )")  # optional just FYI
s4 <- str_squish(s3)

2.F. This white space is then filled with a comma.

s5 <- gsub('\" \"', '\",\"', s4)

2.G. Finally, we use paste0 to put in the “c(” and the “)”.

s6 <- paste0("c(", s5, ")")

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

(.)\1\1
"(.)(.)\\2\\1"
(..)\1
"(.).\\1.\\1"

Answer:

(.)\1\1         :  Any 2 characters that repeats in reverse order.
"(.)(.)\\2\\1"  :  Any 2 characters that res4peats in the same order.
(..)\1          :  Any single character that repeats 2 more times, with each repetition after another single varable character.
"(.).\\1.\\1"   :  Any 3 characters that repeat in the reverse order after any number of variable charaters.

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.)
df.names <- c("bob","juj", "church", "eleven")
answer_1 = "^(.)((.*\\1$)|\\1?$)"
str_subset(df.names, answer_1)
## [1] "bob" "juj"
answer_2 = "([A-Za-z][A-Za-z]).*\\1"
str_subset(df.names, answer_2)
## [1] "church"
answer_3 = "([A-Za-z]).*\\1.*\\1"
str_subset(df.names, answer_3)
## [1] "eleven"