-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.Rmd
348 lines (229 loc) · 7.34 KB
/
index.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
344
345
346
347
---
title: "tidy data and tidy tools"
subtitle: "Overview of tools of effective data analysis"
author: "Eric Leung"
date: "2020-05-17"
output:
xaringan::moon_reader:
self_contained: no
css: xaringan-themer.css
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: true
---
```{r setup, include=FALSE, warning=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(
message = FALSE,
echo = TRUE
)
# Load libraries
library(xaringanthemer)
# Set up styling
style_mono_accent(
base_color = "#242943",
header_font_google = google_font("Josefin Sans"),
text_font_google = google_font("Montserrat", "300", "300i"),
code_font_google = google_font("Fira Mono")
)
```
# Overview
- Tidy data
- Core and useful tidyverse functions
- Useful packages
---
# Tidy data overview
![Tidy data](https://d33wubrfki0l68.cloudfront.net/6f1ddb544fc5c69a2478e444ab8112fb0eea23f8/91adc/images/tidy-1.png)
*Source*: https://r4ds.had.co.nz/tidy-data.html
---
# Tidy data are easier to work with inside the tidyverse
> "Tidy datasets are all alike, but every messy dataset is messy in its own
way."
>
> –– Hadley Wickham
---
# Objective
Take built-in phyloseq object and convert it to a tidy data set. Then wrangle
the data using tidy principles and tidyverse packages<sup>*</sup>.
.footnote[[*] The "tidyverse" packages are a suite of packages that adhere to
tidy data principles]
---
# Load packages
```{r}
library(phyloseq)
# install.packages("remotes")
# remotes::install_github("mikemc/speedyseq")
library(speedyseq) # Optional
```
---
# phyloseq data example
```{r load_pkgs_data}
# Load data example
data("GlobalPatterns")
GlobalPatterns
```
--
Let's convert this into a tidy data set.
---
# `psmelt()` converts to tidy data
```{r melt_phyloseq}
psm_gp <- psmelt(GlobalPatterns) # Create tidy data
```
--
```{r load_convert_tibble, warning=FALSE}
library(tibble)
psm_gp <- tibble(psm_gp) # Make nicer data frame
```
--
```{r explore_melted_df}
head(psm_gp, 5) # View first five rows
```
---
# Quick note on the tidyverse
- The {tidyverse} package is a suite of packages
- Home website https://www.tidyverse.org/
- Running `library(tidyverse)` will load the main tidyverse packages
- The following code loads the appropriate packages within the tidyverse when
needed
---
# Let's clean the `Descriptions` column
```{r dplyr_basics, warning=FALSE}
library(dplyr)
psm_gp %>%
select(OTU, Sample, SampleType, Description) %>%
head(5)
```
---
# Just keep stool samples
```{r}
psm_gp %>%
select(OTU, Sample, SampleType, Description) %>%
filter(SampleType == "Feces") %>%
head(5)
```
--
The descriptions have lots of information in them that we might want to
visualize later on with ggplot2.
--
Difficult to plot when the data is locked in text form.
**Note**: some of the following code may not be "best" design decision
---
# Load core data wrangling libraries
```{r load_tidyr, warning=FALSE}
library(tidyr)
```
--
```{r}
# Column must be character, `Description` is currently a factor
psm_gp %>%
select(OTU, Sample, SampleType, Description) %>%
filter(SampleType == "Feces") %>%
mutate(Description = as.character(Description)) %>% #<<
separate_rows(Description, sep = ", ") %>% #<<
head(5)
```
---
# Let's move values into columns
```{r}
# Create `value` column to keep value
psm_gp %>%
select(OTU, Sample, SampleType, Description) %>%
filter(SampleType == "Feces") %>%
mutate(Description = as.character(Description)) %>%
separate_rows(Description, sep = ", ") %>%
mutate(value = 1) %>% #<<
pivot_wider(names_from = Description, #<<
values_from = value) %>% #<<
head(5)
```
---
# We can reverse our pivot
```{r}
psm_gp %>%
select(OTU, Sample, SampleType, Description) %>%
filter(SampleType == "Feces") %>%
mutate(Description = as.character(Description)) %>%
separate_rows(Description, sep = ", ") %>%
mutate(value = 1) %>%
pivot_wider(names_from = Description,
values_from = value) %>%
pivot_longer(cols = !c(OTU, Sample, SampleType), #<<
names_to = "Description", #<<
values_to = "Value") %>% #<<
head(5)
```
---
# Future steps
- With the code here, you could further process the data to filter on specific
features.
- For example, you could focus on stool samples from a specific twins or a whole
body study.
- The {stringr} package within the tidyverse will prove useful for checking the
contents of strings
---
# Functions reviewed
- **Note**: the following functions are in the form `<package>::<function>`
- **`dplyr::select()`** = select, subset, and remove columns from data frame
- **`dplyr::filter()`** = remove or keep specific rows based on information in
data frame
- **`dplyr::mutate()`** = convert or transform column values
- **`tidyr::separate_rows()`** = take column values and separate them into
multiple rows
- **`tidyr:pivot_wider()`** and **`tidyr::pivot_longer()`** = transform data
frame from long to wide data
---
# Useful packages
- janitor = miscellaneous cleaning
- unheadr = weird nested headers
- tidycells = multiple tables spaced out in Excel
---
# janitor for cleaning columns and creating frequency tables
```{r show_janitor, warning=FALSE}
library(janitor)
psm_gp %>%
select(OTU, Sample, SampleType, Description) %>%
filter(SampleType == "Feces") %>%
mutate(Description = as.character(Description)) %>%
separate_rows(Description, sep = ", ") %>%
clean_names() %>% #<<
head(5)
```
---
# janitor for creating frequency tables
```{r show_freq_tabs}
psm_gp %>%
select(OTU, Sample, SampleType, Description) %>%
tabyl(SampleType) %>% #<<
adorn_totals() #<<
```
---
# unheadr for nested headers
**Before**
| scientific\_name | common\_name | red\_list\_status | mass\_kg |
| :---------------------- | :--------------------------- | :---------------- | -------: |
| Asia | NA | NA | NA |
| CERCOPITHECIDAE | NA | NA | NA |
| Trachypithecus obscurus | Dusky Langur | NT | 7.13 |
| Presbytis sumatra | Black Sumatran Langur | EN | 6.00 |
---
# unheadr for nested headers
**After**
| scientific\_name | common\_name | red\_list\_status | mass\_kg | family |
| :---------------------- | :--------------------------- | :---------------- | -------: | :-------------- |
| Trachypithecus obscurus | Dusky Langur | NT | 7.13 | CERCOPITHECIDAE |
| Presbytis sumatra | Black Sumatran Langur | EN | 6.00 | CERCOPITHECIDAE |
---
# tidycells for odd cell arrangements
![](https://r-rudra.github.io/tidycells/articles/ext/marks.png)
---
# Resources
- R for Data Science https://r4ds.had.co.nz
- Ted's list of underrated tidyverse functions
https://hugo-portfolio-example.netlify.app/projects/tidyverse_functions/
- {janitor} R package https://github.com/sfirke/janitor
- {unheadr} R package https://github.com/luisDVA/unheadr
- {tidycells} https://r-rudra.github.io/tidycells/
- Blog post on cleaning messy data
https://rfortherestofus.com/2019/12/how-to-clean-messy-data-in-r/