-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcm101.Rmd
149 lines (120 loc) · 2.67 KB
/
cm101.Rmd
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
---
title: "cm101"
author: "Cody"
date: "October 24, 2017"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# R for strings
```{r}
my_string <- "That's cool"
my_string
my_string2 <- 'This is another string with "double quotes". That\'s cool'
my_string2
my_string3 <- "A slash \\ "
my_string3
```
### Homework
How do I "string" a single slash?
```{r}
my_string4 <- `my_string`
my_string5 <- 'my_string'
my_string4==my_string5
my_string
```
Back quotes `` will allow for white space. This is useful for using datasets with strings.
```{r}
library(singer)
places <- unique(singer_locations$city)
str(places)
nchar(c("City","double", "a very long string"))
nchar(c("",NA," "," ","\t"," "))
```
Note: Tab is represented by \t or just a tab, and they have different values under nchar. Quite strange.
## The tools of the trade
```{r}
devtools::install_github("tidyverse/stringer")
devtools::install_github("tidyverse/glue")
library(tidyverse)
library(stringer)
library(glue)
```
glueing 'glue{}'
```{r}
my_name <- "Cody"
my_age <- 013
my_color <- "Orange"
glue("My name is {my_name},", " My age yesterday was {my_age-1/365}")
paste0("My name is ",my_name,", My age yesterday was ",my_age-1/365)
```
Indentation
```{r}
glue("
My name is
{my_name},
My age yesterday was
{my_age-1/365}")
```
Phrase : Today is Wedneday, October 29, 1986 and the sun shrines
Sys.date()
glue them togther
```{r}
phrase <- "Today is {Today_date} and the sun shrines"
glue(phrase,
Today_date=Sys.Date())
glue(phrase,
Today_date=(format(Sys.Date(), "%A, %B %d, %Y")))
```
Glue data
```{r}
singer_locations %>%
glue_data("{artist_name} is from {city}.") %>%
head()
```
# Stringr
Here are all the functions from this package
```{r}
library(stringr)
install.packages("htmlwidgets")
library(htmlwidgets)
getNamespaceExports("stringr")
```
Now lets say we want only the functions that have str
```{r}
getNamespaceExports("stringr") %>%
str_view("str_")
getNamespaceExports("stringr") %>%
str_subset("str_")
```
Lets find all the places containing th estring "york"
```{r}
places <- unique(singer_locations$city)
places %>%
str_to_lower() %>%
str_subset("york")
```
# Regular Expressions
```{r}
places %>%
str_subset("[Yy]ork")
places %>%
str_subset(coll("york",ignore_case=TRUE))
places %>%
str_subset("York$")
places %>%
str_subset("^York")
```
Find all places that contain a digit in their name
```{r}
places %>%
str_subset("[:digit:]")
```
B.a means must begin with B and have one a after B.
B.a? means either 0 or 1 a must happen after B.
B.a+ means multiple a can come after B.
```{r}
places %>%
str_subset("B.a+")
```