Week 3 Assignment - Data 607 MSDS

-Loading the necessary libraries

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) 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”

-Loading the data set

majors <- read.csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/college-majors/majors-list.csv",header = TRUE, sep = ",")

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

Filtering and printing the rows where the Major column from the “majors” data frame contains either “DATA” or “STATISTICS”.

majors %>% 
  filter(str_detect(Major, regex("statistics|data", ignore_case = TRUE))) %>% 
  print()
##   FOD1P                                         Major          Major_Category
## 1  6212 MANAGEMENT INFORMATION SYSTEMS AND STATISTICS                Business
## 2  2101      COMPUTER PROGRAMMING AND DATA PROCESSING Computers & Mathematics
## 3  3702               STATISTICS AND DECISION SCIENCE Computers & Mathematics

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_and_veggies <- '[1] "bell pepper"  "bilberry"     "blackberry"   "blood orange"

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

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

[13] "olive"        "salal berry"'

fruits_and_veggies
## [1] "[1] \"bell pepper\"  \"bilberry\"     \"blackberry\"   \"blood orange\"\n\n[5] \"blueberry\"    \"cantaloupe\"   \"chili pepper\" \"cloudberry\"  \n\n[9] \"elderberry\"   \"lime\"         \"lychee\"       \"mulberry\"    \n\n[13] \"olive\"        \"salal berry\""

Use the str_extract_all function with the ‘[A-Za-z]+.?[A-Za-z]+’ pattern to detect single words and possible set of two words with a space in between. Next convert the string into a vector.

fruitveggies_vector <-  unlist(stringr::str_extract_all(fruits_and_veggies, pattern ='[A-Za-z]+.?[A-Za-z]+') )
fruitveggies_vector
##  [1] "bell pepper"  "bilberry"     "blackberry"   "blood orange" "blueberry"   
##  [6] "cantaloupe"   "chili pepper" "cloudberry"   "elderberry"   "lime"        
## [11] "lychee"       "mulberry"     "olive"        "salal berry"

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

(.)\1\1 This expression matches three occurences in a row of a specific character or string text

“(.)(.)\2\1” This expression will match four characters where the first and fourth characters are the same, and the second and third characters are the same.

(..)\1 This expression will match two characters where any character is immediately followed by that same character

“(.).\1.\1” This expression will match three identical characters separated by one identical character of any kind between them.

“(.)(.)(.).*\3\2\1” This expression will match three characters followed by those same three characters but in reverse order

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

testing_vector <- c("apple", "banana", "church", "eleven", "bob", "pepper", "mississippi", "noon", "level", "radar", "deified", "redivider", "madam", "civic", "rotor")

# Start and end with the same character.
pattern_1 <- "^(.).*\\1$"
str_extract(testing_vector, pattern_1)
##  [1] NA          NA          NA          NA          "bob"       NA         
##  [7] NA          "noon"      "level"     "radar"     "deified"   "redivider"
## [13] "madam"     "civic"     "rotor"
# Contain a repeated pair of letters (e.g. "church" contains "ch" repeated twice.)
pattern_2 <- "([a-zA-Z]{2}).*\\1"
str_extract(testing_vector, pattern_2)
##  [1] NA       "anan"   "church" NA       NA       "peppe"  "issis"  NA      
##  [9] NA       NA       NA       NA       NA       NA       NA
# Contain one letter repeated in at least three places (e.g. "eleven" contains three "e"s.)

pattern_3 <- "\\b(?:[a-zA-Z]*([a-zA-Z])(?:[^\\1]*\\1){2,}[a-zA-Z]*){1,}\\b"
str_extract(testing_vector, pattern_3)
##  [1] NA            "banana"      NA            "eleven"      NA           
##  [6] "pepper"      "mississippi" NA            NA            NA           
## [11] NA            NA            NA            NA            NA