-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcodeForClass4.R
More file actions
118 lines (95 loc) · 2.43 KB
/
codeForClass4.R
File metadata and controls
118 lines (95 loc) · 2.43 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
if (!require(tidyverse)) install.packages("tidyverse")
library(stringr)
# length
str_length("abc")
str_length("한글")
# subset
x <- c("abcdef", "ghifjk")
str_sub(x, 3, 3)
str_sub(x, 2, -2)
# replace subset
str_sub(x, 3, 3) <- "X"
x
# duplicate
str_dup(x, c(2, 3))
# padding
x <- c("abc", "defghi")
str_pad(x, 10)
str_pad(x, 10, "both")
str_pad(x, 4)
# truncate text
x <- c("Short", "This is a long string")
x %>%
str_trunc(10) %>%
str_pad(10, "right")
# remove blank
x <- c(" a ", "b ", " c")
str_trim(x)
str_trim(x, "left")
# locale sensitive function
x <- "I like horses."
str_to_upper(x)
str_to_title(x)
str_to_lower(x)
# ordering
x <- c("y", "i", "k")
str_order(x)
str_sort(x)
# set sample data
strings <- c(
"apple",
"219 733 8965",
"329-293-8753",
"Work: 579-499-7527; Home: 543.355.3679"
)
phone <- "([2-9][0-9]{2})[- .]([0-9]{3})[- .]([0-9]{4})"
# match functions
str_subset(strings, phone)
str_detect(strings, phone)
str_count(strings, phone)
# which locate
str_locate(strings, phone)
str_locate_all(strings, phone)
# extract matching text
str_extract(strings, phone)
str_extract_all(strings, phone)
str_extract_all(strings, phone, simplify = TRUE)
# return match and matching group
str_match(strings, phone)
str_match_all(strings, phone)
# replace
str_replace(strings, phone, "XXX-XXX-XXXX")
str_replace_all(strings, phone, "XXX-XXX-XXXX")
# split strings
str_split("a-b-c", "-")
str_split_fixed("a-b-c", "-", n = 2)
if (!require(nycflights13)) install.packages("nycflights13")
library(nycflights13)
head(airports, 3)
grep("^New", airports$name, value = T)
airports[grep("^New", airports$name, value = T), "name"]
grep("[0-9]$", airports$name, value = T)
grep("[[:digit:]]$", airports$name, value = T)
x <- c("apple", "banana", "pear", "aple", "abble","appppppppple")
grep("app*", x)
grep("app?", x)
grep("app+", x)
y <- c("a","aa","aaa","aaaa","aaaaa")
grep("a{3}", y)
stri<-"<p> <em>안녕</em>하세요 </p><p>테스트입니다.</p>"
sub("<p>.*</p>","tar",stri)
sub("<p>.*?</p>","tar",stri) ## By adding ?, shorter one is prefered
sub("<p>.?*</p>","tar",stri)
sub("<p>.*?</p>","tar",stri)
sub("<p>.?</p>","tar",stri)
gsub("<p>.*?</p>","tar",stri)
gsub(",", "", "100,000,000,000,000,000") %>%
as.numeric
as.numeric(gsub(",", "", "100,000,000,000,000,000"))
?regexpr
?apropos
apropos("air*")
apropos("air+")
apropos("^air+")
postcodes <- c("155-030", "23078", "324-686")
grep("^[0-9]{3}([0-9]{2}|-[0-9]{3})$", postcodes)