-
Notifications
You must be signed in to change notification settings - Fork 1
/
stringr.Rmd
172 lines (121 loc) · 4.25 KB
/
stringr.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
# Strings manipulation with `stringr`
The `stringr` package provides tools for **string manipulation**.
<br>
All functions in `stringr` start with **str_** and take a vector of strings as the first argument.
<br><br>
We will show here a few useful functions (for a complete list of `stringr` functions, you can have a look at the [Cheat sheet](https://github.com/rstudio/cheatsheets/blob/master/strings.pdf). The cheat sheet also provides guidance on how to work with **regular expressions**.
<br><br>
Let's take a simple **character vector** and a small **tibble** as examples:
```{r, message=FALSE}
examplestring <- c("genomics", "proteomics", "proteome", "transcriptomics", "metagenomics", "metabolomics")
exampletibble <- tibble(day=c("day0", "day1", "day2"),
temperature=c("25C", "27C", "24Celsius"))
```
***
* `str_detect`: detects the presence or absence of a pattern in a string.
```{r}
str_detect(examplestring,
pattern="genom")
```
You can use regular expressions: as a simple example, here we want to detect which element of `examplestring` **starts** with **genom**.
```{r}
str_detect(examplestring,
pattern="^genom")
```
You can reverse the search and output elements where the pattern is NOT found with `negate=TRUE`
```{r}
str_detect(examplestring,
pattern="genom",
negate=TRUE)
```
***
* `str_length`: outputs length of strings (number of characters) in each element of a vector.
```{r}
str_length(examplestring)
```
***
* `str_replace`: looks for a pattern in a string and replace it.
We can replace "omics" with "ome"
```{r}
str_replace(examplestring,
pattern="omics",
"ome")
```
`str_replace` can be used to remove selected patterns from strings:
```{r}
str_replace(examplestring,
pattern="omics",
"")
# str_remove is a wrapper for the same thing (no need for the 3rd argument)
str_remove(examplestring,
pattern="omics")
```
Same with a **tibble's column**:
```{r}
str_remove(exampletibble$day,
pattern="day")
```
You can use it inside another `tidyverse` function:
```{r}
mutate(exampletibble, day=str_remove(day, pattern="day"))
```
***
* `str_count`: count the number of occurences of a pattern:
Count how many times "omics" is found in each element:
```{r}
str_count(examplestring,
pattern="omics")
```
Count how many vowels are found in each element:
```{r}
str_count(examplestring,
pattern="[aeiouy]")
```
***
* `str_sub`: extracts and replace substrings from a character vector
```{r}
str_sub(examplestring,
start=1, # position of the first character
end=10) # position of the last character
```
Let's keep the first 2 characters of the **temperature** column of our `exampletibble`:
```{r}
str_sub(exampletibble$temperature,
start=1,
end=2)
```
Within `mutate`:
```{r}
mutate(exampletibble, temperature=str_sub(temperature, start=1, end=2))
```
<center><h4 style="background-color: #a4edff; display: inline-block;">**HANDS-ON**</h4></center>
We will play with the following **character vector**:
```{r}
countries <- c("Germany", "Uganda", "Canada", "Australia", "Switzerland", "Thailand", "Bolivia", "Russia", "Italy", "Senegal", "South Korea", "Mexico", "Argentina", "England")
```
* What is the **average length** of the country names?
* How many country names **end with an "a"**?
* Replace **empty spaces with underscores** in `countries`.
* In which country name do you find more the **letter "a"**?
<details>
<summary>
<h5 style="background-color: #a4edff; display: inline-block;">*Answer*</h5>
</summary>
```{r, eval=F}
# What is the average length of the country names?
mean(str_length(countries))
# How many country names end with an "a"?
# Get the logical vector
str_detect(countries, "a$")
# Retrieve only the "TRUE" and count
length(which(str_detect(countries, "a$")))
length(countries[str_detect(countries, "a$")])
# Replace empty spaces with underscores in `countries`.
str_replace(countries, " ", "_")
# In which country name do you find more the letter "a"?
# count how many "a" per country name
str_count(countries, "a")
# extract the country name where there are more "a".
countries[max(str_count(countries, "a"))]
```
</details>