#INTRODUCTION In this assignment I will work to extract string data from a fivethirtyeight article. I will create a table and pull out majors pertaining to “DATA or”Statistic”
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.2
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.5.0
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## Warning: package 'stringr' was built under R version 4.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
#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”
college_majors <- read.csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/college-majors/majors-list.csv")
college_majors %>% filter(str_detect(Major, "DATA|STATISTICS")) %>%
pull(Major) %>% c()
## [1] "MANAGEMENT INFORMATION SYSTEMS AND STATISTICS"
## [2] "COMPUTER PROGRAMMING AND DATA PROCESSING"
## [3] "STATISTICS AND DECISION SCIENCE"
#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”
frutas <- data.frame(c("bell pepper", "bilberry", "blackberry","blood orange","blueberry","cantalope","chili pepper","cloudberry","elderberry","lime","lychee","mulberry","olive","salal berry"))
cat(paste0(frutas))
## c("bell pepper", "bilberry", "blackberry", "blood orange", "blueberry", "cantalope", "chili pepper", "cloudberry", "elderberry", "lime", "lychee", "mulberry", "olive", "salal berry")
#3 Describe, in words, what these expressions will match:
(.)\1\1 = to match a pattern in a strings with character repeats three times or more
“(.)(.)\2\1” = match strings with a set of 4 characters, also to match with 2 characters in the same 2 characters in reverse
(..)\1 = to match any strings that have a repeated pair of letters
“(.).\1.\1” = to match any strings that have a repeated pair of characters
“(.)(.)(.).*\3\2\1” = to match any character strings with any 3 characters that repeat in the reverse
#4 Construct regular expressions to match words that:
Start and end with the same character:
“^(.)((.*\1\()|\\1?\))”
exp <- c("bob", "mom", "dad", "son", "baby", "drool", "pool", "spool", "cool", "shush", "banana")
str_subset(exp, "^(.)((.*\\1$)|\\1?$)")
## [1] "bob" "mom" "dad"
Contain a repeated pair of letters (e.g. “church” contains “ch” repeated twice.):
“([A-Za-z][A-Za-z]).*\1”
str_subset(exp,"([A-Za-z][A-Za-z]).*\\1")
## [1] "shush" "banana"
Contain one letter repeated in at least three places (e.g. “eleven” contains three “e”s.) “([A-Za-z]).\1.\1”
str_subset(exp, "([A-Za-z]).*\\1.*\\1")
## [1] "banana"