-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringrFun.Rmd
368 lines (214 loc) · 6.99 KB
/
stringrFun.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
---
title: "Using Stringr for strings"
output:
html_document:
toc: yes
code_folding: show
theme:
bg: "#202123" # black
fg: "#B8BCC2" # grey
primary: "#EA80FC" # purple
pre: "#99ff66"
code: "#99ff66"
span: "#99ff66"
a: "#66ffcc"
base_font:
google: Ubuntu
heading_font:
google: Raleway
version: 3
---
```{r setup, include=FALSE}
if (requireNamespace("thematic"))
thematic::thematic_rmd(font = "auto")
```
## Load stringr
Text are strings and the R package stringr and/ stringi are libraries to help deal with string data as part of data analysis.
Note:*Text string color is red and is not customizable, I tried to have another color while having a dark mode.*
```{r echo=TRUE, warning=FALSE, message=FALSE, include=TRUE}
library(stringi)
library(stringr)
library(tidyverse)
# stringr had built in data
fruit = stringr::fruit
head(fruit)
head(sentences)
```
## Text strings
Messy text
```{r}
txt = "YOU CAN HAVE ALL CAPS DATA edit edit edit"
text2 = "// YoU nEvEr kNöw //"
text3 = "2-009.89.0."
text4 = "<p> ...Text strings </p> "
text5 = "|| * * * <br> \n didyouseeThis?! {stringr} z = 6 *9"
text6 = "http://www.link.com/strings"
text7 = "bill total $400,35 [07/08/01]"
folder = c("results-001-2020.csv", "results-002-2020.csv", "results-003-2020.csv", "results-004-2020.csv", "results-005-2020.csv")
```
## Stringr Functions
Will use fruits data and text strings.
### detect matches
Detection methods return Boolean array or index location
- `str_detect(string, pattern) `
- `str_detect(fruit, pattern = "a")`
- `str_detect(txt, pattern = "<h1>")`
- `str_starts(fruit, pattern = "ap" )`
```{r}
str_starts(text2, pattern = "//")
```
More detection functions:
- `str_which()` finds the indexes of strings that contain match
- `str_locate()` locates the positions of pattern match
- `str_locate_all(fruit, pattern = "an")`
- `str_coount() `count the number of matches in string
- `str_count(fruit, pattern = "an")`
```{r}
str_which(fruit, pattern = "ma")
```
### Subset strings
- `str_sub(string, start= , end= )` extract substrings from character vector
- `str_subset(string, pattern= )` returns only string in pattern match
- `str_extract()`/ `str_extract_all()` returns 1st match in string as a vector / returns all matches
- `str_match()` / `str_match_all()` return 1st pattern match as a matrix
```{r}
str_sub(text2, start = 4, end = 17)
str_subset(fruit, pattern = "bl")
str_extract_all(txt, "edit")
```
### manage string lengths
- `str_length(string)` returns length (width) of entire strings
- `str_pad(string, width= , side= c('left','right','both') ')` add string apdding
- `str_trunc(string, width= , side= )` truncate a string
- `str_squish(string)` trim whitespace from each end
```{r}
str_length(txt)
str_pad(fruit[1], width=15, side='left') # pad on left side of 1st element in fruits
sentences[1]
str_trunc(sentences[1], width = 17, side='right')
str_squish(sentences[1])
```
### Mutate strings
- `str_replace(string, pattern= ,replacement= )` / `str_replace_all()`
- `str_to_lower()` converts strings to lowercase
- `str_to_upper()` converts strings to uppercase
- `str_to_title()` convert strings to title case
```{r}
str_replace(text2, pattern = "nEvEr", replacement = "always")
str_to_lower(txt, locale = 'en')
str_to_upper(txt)
str_to_title(txt)
```
### Join & Split strings
- `str_c(string1, string2, sep= )` join multiple strings into single string
- `str_flatten(string, collapse= )` combines into a single string
- `str_dup(string, times= n)` repeat strings by **n** times
- `str_split_fixed(string, pattern= , n= )` /`str_split()` / `str_split_()`
- `str_glue()` create string from strings and {expressions}
- `str_glue_data()` use a dataframe/ list to create a string from strings and {expressions}
```{r}
str_c(letters[1:5], LETTERS[1:5], sep = "^")
str_c(txt, text4, sep = " :: ")
str_flatten(fruit[1:5], collapse = " % ")
str_dup(text3, times = 3)
str_split_fixed(txt, pattern = "YOU", n= 3)
str_glue("Pi is {pi}")
str_glue(text2, text7)
# starwars data is from dplyr
str_glue_data(head(starwars), "starwars has {head(name)}")
```
### Order Strings
```{r}
str_order(letters[1:10], decreasing = F, numeric = T)
str_sort(letters[10:1])
```
### Helpers
`str_view_all(fruit, pattern = "berry")` returns a page with highlight matches.
```{r}
# override existing encoding of letter
str_conv(fruit[1:4], encoding = "ISO-8859-1")
str_wrap(txt, width = 5, indent = 2, exdent = 3)
str_wrap(letters, width = 5, indent = 7)
```
## Regular Expressions (RegEx)
This section is **need to know** :
- regular expressions go in " "
- special characters, run `?"'"` in RStudio terminal for full list
- `\\` = `\`
- `\"` = "
- `\n` = new line
- use `writeLines("\\.")` to see what is viewed
### Match character
To match a specific character, use `"\\{char}"` example `"\\!"`
- {char} = . ! ? ( ) `{` `}` `\n` `\t`
- `\d` (any digit)
- `\w` (any word character)
- need 4 backslashes for 1 back slash `\\\\`
Note that the '.' expression returns everything except for new line.
### RegEx Helpers
- `"[:digit:]"` matches digits
- `"[:letters:]"` matches letters
- `"[:lower:]"` matches lowercase letters
- `"[:upper:]"` matches uppercase letters
- `"[:alnum:]"` matches letters and numbers
- `"[:punct:]"` matches punctuation
- `"[:graph:]"` matches letters, numbers, punctuation
- `"[:space;]"` matches space characters
- `"[:blank:]"` matches space and tab (not new line)
```{r}
expressions = c("abc ABC 123\t!?\\(){}\n")
str_extract_all(text7, "[:digit:]")
str_extract_all(text2, "[:alnum:]")
str_extract_all(text2, "[:lower:]")
str_extract_all(text6, "[:alpha:]")
```
### Alternates
Custom searches of strings
```{r}
# match A or B
# str_match(fruit, "apple|pear")
str_count(text3, "0|9")
# match one of
str_match_all(text3, "[09]")
# match anything but
str_extract_all(text6, "[^http://]")
# match a string between a range
str_extract_all(text4, "[Text - strings]")
```
### Quantifiers
```{r}
# match 0 or more
str_match_all(txt, "edit?")
str_match(folder, "2020?")
# match ZERO or more
str_match(text3, "8*")
# match exactly n times
str_match_all(text6, "w[2]")
# match n or more times
str_match_all(txt, "edit[2,]")
# match between n and m times
str_match_all(text3, "0[1,3]")
```
### Anchors !
This is the specialized part
```{r}
# match at start of string
str_count(folder, "^results")
# match at end of string
str_detect(folder, ".csv$")
```
In the folder vector is a list of csv files, to extract each of them properly need to use special formatting. In the pattern `\\d{n}` d is for digit and there are 3 digits after results and 4 digits after that. This is useful when running a `purrr` function.
```{r}
folder
str_extract_all(folder, "^results-\\d{3}-\\d{4}.csv$")
```
### Look arounds
```{r}
s ="bacad"
# match followed by a char
str_match(s, "a(?=c)")
# match not followed by a char
str_extract_all(s, "a(?!c)")
# match preceded by
str_extract_all(s, "(?<!b)a")
```