-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSBV_Assignment3_CharacterManipulationDataProcessing.Rmd
More file actions
159 lines (144 loc) · 4.5 KB
/
Copy pathSBV_Assignment3_CharacterManipulationDataProcessing.Rmd
File metadata and controls
159 lines (144 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
title: "Week 3 Assignment - Character manipulation & Data processing"
author: "Stefano Biguzzi"
date: "9/9/2020"
output:
html_document:
includes:
in_header: header.html
css: ./lab.css
highlight: pygments
theme: cerulean
toc: true
toc_float: true
pdf_document: default
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = TRUE)
library(tidyverse)
library(RCurl)
library(knitr)
library(kableExtra)
library(stringr)
```
# Part I:
**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"**
```{r rawdata, echo=TRUE, include=FALSE}
rawdata <- getURL(
"https://raw.githubusercontent.com/fivethirtyeight/data/master/college-majors/majors-list.csv"
)
major_data <- read.csv(text = rawdata)
```
```{r filteredata}
filtered_data <-
major_data[
grepl("(DATA|STATISTICS)",major_data$Major),
]
```
```{r table, echo=FALSE}
rownames(filtered_data) <- NULL
kable(
filtered_data,
caption = "Filtered Majors",
booktabs = T
) %>% kable_styling(
latex_options = c("scale_down","hold_position")
)
```
# Part II:
**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"
```{r createlist, echo=FALSE}
fruit_list <- c(
"bell pepper",
"bilberry",
"blackberry",
"blood orange",
"blueberry",
"cantaloupe",
"chili pepper",
"cloudberry",
"elderberry",
"lime",
"lychee",
"mulberry",
"olive",
"salal berry"
)
```
```{r printfruitlist, comment=NA}
fruit_list
```
**Into a format like this:**
c("bell pepper", "bilberry", "blackberry", "blood orange", "blueberry", "cantaloupe", "chili pepper", "cloudberry", "elderberry", "lime", "lychee", "mulberry", "olive", "salal berry")
```{r transformprint, comment=NA}
dput(fruit_list)
```
# Part III:
**Describe, in words, what these expressions will match:**
```{r addingtofruitlist, include=FALSE}
fruit_list <- append(
fruit_list,
c("apple", "banana","appleelppa")
)
```
* (.)\1\1
* This will present an error in two ways. First, there are no quotes around the pattern. Secondly, with only one '\\' Regex will read (.)\1\1 as (.) instead of (.)\1\1.
```{r fruitlisterror, echo=FALSE, error=TRUE, comment=NA}
str_view(fruit_list,(..)\1)
```
* "(.)(.)\\\\2\\\\1"
* Take one character, followed by another character, repeat the second character, then the first. This is equivalent to ABBA.
```{r fruitlistabba, echo=FALSE}
str_view(fruit_list,"(.)(.)\\2\\1")
```
* (..)\1
* This will also give you an error as it is not in quotes. Additionally, to get the regex right it should be \\1 and not \1 as regex again will read it as (..) instead of (..)\\1.
```{r fruitlisterror2, echo=FALSE, error=TRUE, comment=NA}
str_view(fruit_list,(..)\1)
```
* "(.).\\\\1.\\\\1"
* This is looking for a character, then any other character, then at the same character again, then any other character, then the same character again. The format would be ABACA
```{r fruitlistabaca, echo=FALSE}
str_view(fruit_list,"(.).\\1.\\1")
```
* "(.)(.)(.).\*\\\\3\\\\2\\\\1"
* This will look at three characters then any number of other characters, followed by the three characters repeated backwards. This would show up as ABCDDDCBA
```{r fruitlistabcdddcba, echo=FALSE}
str_view(fruit_list,"(.)(.)(.).*\\3\\2\\1")
```
# Part IV:
**Construct regular expressions to match words that:**
```{r appendfruit, echo=FALSE}
fruit_list <- append(
fruit_list,
c(
"church",
"assassin",
"bookkeeper",
"eleven",
"starts"
)
)
```
* Start and end with the same character.
* "^(.).\*\\\\1$"
```{r abcda, echo=FALSE}
str_view(fruit_list,"^(.).*\\1$")
```
* Contain a repeated pair of letters (e.g. "church" contains "ch" repeated twice.)
* "([A-Za-z][A-Za-z]).\*\\\\1"
```{r abstuffab, echo=FALSE}
str_view(fruit_list,"([A-Za-z][A-Za-z]).*\\1")
```
* Contain one letter repeated in at least three places (e.g. "eleven" contains three "e"s.)
* "([A-Za-z]).\*\\\\1.\*\\\\1"
```{r 3-repeat, echo=FALSE}
str_view(fruit_list,"([A-Za-z]).*\\1.*\\1")
```
* * *