This repository was archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path02_subsetting.Rmd
More file actions
308 lines (196 loc) · 4.53 KB
/
02_subsetting.Rmd
File metadata and controls
308 lines (196 loc) · 4.53 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
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
---
title: 'Subsetting'
author: "D-Lab"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
df_print: paged
number_sections: yes
toc: yes
toc_float: yes
pdf_document:
toc: yes
---
```{r include = FALSE}
# p_load loads and, if necessary, install missing packages.
# install.packages() + library() = p_load()
# If you just want to install, then use p_install()
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
tidyverse, # for the tidyverse framework
palmerpenguins,
gapminder,
kableExtra,
flextable,
modelr,
nycflights13
)
```
# Subset Observations (Rows)
```{r}
penguins <- palmerpenguins::penguins
penguins
```
## Choose row by logical condition
- Single condition
```{r}
penguins %>%
filter(species == "Adelie") %>%
arrange(desc(bill_length_mm))
```
The following filtering example was inspired by [the suzanbert's dplyr blog post](https://suzan.rbind.io/2018/02/dplyr-tutorial-3/).
- Multiple conditions (numeric)
```{r}
# First example
penguins %>%
filter(bill_length_mm <= 42, bill_length_mm > 35) %>%
nrow()
```
```{r}
# Same as above
penguins %>%
filter(bill_length_mm <= 42 & bill_length_mm > 35) %>%
nrow()
```
```{r}
# Not same as above
penguins %>%
filter(bill_length_mm < 42 | bill_length_mm > 35) %>%
nrow()
```
**Challenge 1**
1. (1) Use `filter(between())` to find characters whose bill lengths are between 35.1 and 42.2 and (2) count the number of these observations.
```{r}
#User code goes here
```
- Multiple conditions (character)
```{r}
# Filter names include ars; `grepl` is a base R function
penguins %>%
filter(grepl("ent", tolower(species)))
```
```{r}
# Or, if you prefer dplyr way
penguins %>%
filter(str_detect(tolower(species), "ent"))
# Filter to Biscoe and Dream Islands
penguins %>%
filter(island %in% c("Biscoe", "Dream"))
```
**Challenge 2**
Use `str_detect()` to find all the penguins whose species includes "Chin"
```{r}
##User Code goes here
penguins %>%
filter()
```
## Choose row by position (row index)
```{r}
penguins %>%
arrange(desc(bill_length_mm)) %>%
slice(1:6)
```
## Sample by fraction
```{r}
# For reproducibility
set.seed(1234)
# Old way
penguins %>%
sample_frac(0.10,
replace = FALSE) # Without replacement
# New way
set.seed(1234)
penguins %>%
slice_sample(prop = 0.10,
replace = FALSE)
```
## Sample by number
```{r}
# Old way
set.seed(1234)
penguins %>%
sample_n(20,
replace = FALSE) # Without replacement
# New way
set.seed(1234)
penguins %>%
slice_sample(n = 20,
replace = FALSE) # Without replacement
```
## Top 10 rows orderd by height
```{r}
# New way
penguins %>%
slice_max(bill_length_mm, n = 10) # Variable first, Argument second
```
# Subset Variables (Columns)
```{r}
names(penguins)
```
### Select only numeric columns
```{r}
# Only numeric
penguins %>%
select(where(is.numeric))
```
**Challenge 3**
Use `select(where())` to find only non-numeric columns
```{r}
#User code goes here
```
## Select the columns that include "bill" in their names
```{r}
penguins %>%
select(contains("bill"))
```
## Select the columns that include either "bill" or "mm" in their names
- Basic R way
`grepl` is one of the R base pattern matching functions.
```{r}
penguins[grepl('bill|mm', names(penguins))]
```
**Challenge 4**
Use `select(matches())` to find columns whose names include either "bill" or "mm".
```{r}
#User code goes here
```
## Select the columns that starts with "b"
```{r}
penguins %>%
select(starts_with("b"))
```
## Select the columns that ends with "mm"
```{r}
penguins %>%
select(ends_with("mm"))
```
## Select the columns using both beginning and end string patterns
The key idea is you can use Boolean operators (`!`, `&`, `|`)to combine different string pattern matching statements.
```{r}
penguins %>%
select(starts_with("b") & ends_with("mm"))
```
## Select bill_length_mm and move it before everything
```{r}
# By specifying a column
penguins %>%
select(bill_length_mm, everything())
```
## Select variables from a character vector.
```{r}
penguins %>%
select(any_of(c("species", "bill_length"))) %>%
colnames()
```
## Select the variables named in the character + number pattern
```{r}
## This uses common dplyr functions to make a new variable.
penguins_example <- penguins %>%
mutate(obs1 = 1,
obs2 = 2,
obs3 = 3,
obs4 = 4,
obs5 = 5)
penguins_example %>%
select(num_range("obs", c(1:4)))
```