-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.Rmd
1105 lines (667 loc) · 20.8 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Data Manipulation with R"
subtitle: "ODA R Learning Series"
author: "Matthew Kumar"
institute: "Bayer"
date: "December 4, 2020"
output:
xaringan::moon_reader:
chakra: libs/remark-latest.min.js
seal: false
fig_caption: false
lib_dir: libs
css: ['assets/css/fc.css', 'assets/css/fc-fonts.css']
nature:
ratio: 16:9
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r, include = F}
knitr::opts_chunk$set(warning = F, comment = "", cache = F, message = F)
#load dependencies
#devtools::install_github("EvaMaeRey/flipbookr")
library(flipbookr)
library(xaringan)
library(flextable)
library(tidyverse)
library(lubridate)
library(janitor)
library(tidyr)
library(ggdark)
library(lubridate)
library(readxl)
#load example data sets
source("assets/scripts/example_data.R")
```
class: inverse, title-slide
<div style= "float:right;position: relative; left: 30px; bottom: -450px;">
```{r echo=FALSE, out.width="150px", fig.align='right'}
knitr::include_graphics("assets/images/cross1.png")
```
</div>
<br>
<br>
<br>
# Data Manipulation with R
## ODA R Learning Series
<br>
### Matthew Kumar
### Bayer
### December 4, 2020
---
class: inverse, bullet
# Agenda
1. Introduction
2. Data manipulation techniques
3. Data tidying techniques
4. Resources
5. Exercises
---
class: inverse, bullet
# The Approach
- Introduce the tidyverse ecosystem
- Provide a tour of essential data manipulation packages and techniques
- Use minimal, <span style="color:#89D329">relatable</span> examples to demonstrate core concepts
- Provide a head start to **continued** learning
---
class: inverse, center, middle
# Tidyverse
---
class: inverse
<div style= "float:right;position: relative; left: 30px; bottom: -250px;">
```{r echo=FALSE, out.width="500px", fig.align='right'}
knitr::include_graphics("assets/images/hex.png")
```
</div>
# Tidyverse
*"The tidyverse is an **opinionated collection of R packages** designed for data science. All packages share an underlying design philosophy, grammar, and data structures."*
<p style="margin-left:13%;color:#89D329">-Tidyverse.org</p>
<br>
<br>
<br>
- Packages play well with each other
- Maintained, tested and documented
- Inspired, growing ecosystem
- Coverage along the data analytic workflow
- `> install.packages("tidyverse")`
---
# A Tidy Workflow
```{r echo=FALSE, out.width="900px", fig.align='center', fig.cap = "Image Credit: https://beanumber.github.io/tidy-databases"}
knitr::include_graphics("assets/images/pipeline.png")
```
---
class: inverse
# Pipe Operator, %>%
- Used in the Tidyverse to "chain" together parts of a pipeline
<br>
<br>
> data %>%
> function_1() %>%
> function_2() %>%
> ...
<br>
- A pipeline typically starts with a data source
- Data is passed as input to a function and is evaluated
- That output is then passed as input to the next function
- Rinse and repeat
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/pipehex.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 200px; height: 200px')
```
---
class: inverse
# Pipe Operator, %>%
```{r echo=FALSE, out.width="500px", out.height="400px", fig.align='center'}
knitr::include_graphics("assets/images/meme.png")
```
---
`r chunk_reveal("code_2", widths = c(1,1), title = "# Pipe Operator, %>%")`
```{r code_2, include = FALSE, comment = ""}
# Data Layer
adsl %>%
filter(SAFFL == "Y") %>%
group_by(TRTP) %>%
summarise(N = n(),
M = mean(AGE),
SD = sd(AGE)) %>%
# Table Layers
flextable() %>%
# Rounding
colformat_num(digits = 2) %>%
# Header
add_header_lines("Table 1: Age by Treatment") %>%
# Footer
add_footer_lines("M = Mean, SD = Standard Deviation ") %>%
# Theme
theme_tron()
```
---
`r chunk_reveal("code_3", widths = c(1.5 ,3), title = "# Pipe Operator, %>%", float="left", chunk_options="fig.width=10, fig.retina=2")`
```{r code_3, include = FALSE, comment = "", message = FALSE, echo=FALSE, warning=FALSE}
# Data Layer
adsl %>%
filter(SAFFL == "Y") %>%
# Plot Layers
ggplot() +
# Mapping
aes(x = HEIGHT,
y = WEIGHT,
fill = TRTP) +
# Point
geom_point(shape = 21,
color = "black",
size = 4) +
# Bayer Colors
scale_fill_manual(values=c("#89D329",
"#FF3162")) +
# Split by TRTP
facet_grid( ~ TRTP) +
# Fit Model
geom_smooth(method = "lm") +
# Change Theme
my_theme()
```
---
`r chunk_reveal("code_4", widths = c(1,1), title = "# Typical %>% Use")`
```{r code_4, include = FALSE, comment = ""}
# Data Processing
step1 <- adsl %>%
filter(SAFFL == "Y") %>%
group_by(TRTP) %>%
summarise(N = n(),
M = mean(AGE),
SD = sd(AGE))
# Inspect, Debug, etc
step1
# Table
table1 <- step1 %>%
flextable() %>%
colformat_num(digits = 2) %>%
add_header_lines("Table 1: Age by Treatment") %>%
add_footer_lines("M = Mean, SD = Standard Deviation ") %>%
theme_tron()
# Inspect, Debug, etc
table1
```
---
class: inverse, center, middle
# Data Manipulation
---
class: inverse
# dplyr
## Grammar of Data Manipulation
- Data manipulation is driven by a set of SQL-like verbs
- `select()`
- `filter()`
- `arrange()`
- `mutate()`
- `summarise()`
- `group_by()`
- Combinations of these can be chained using the **%>%**
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/dplyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
class: inverse, bullet
# dplyr
## select()
- The `select()` verb is used to choose columns
- Can specify selections in flexible ways
- Similar to a SAS `keep` statement or PROC SQL `select` statement
- Provisions for wild card select (SAS `:`)
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/dplyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
`r chunk_reveal("dplyr_select", break_type = "rotate", widths = c(0.75,1.25), title = "### select()")`
```{r dplyr_select, eval = F, echo = F}
# Base Data
mini_adsl
# Selected Data
mini_adsl %>%
select(SUBJIDN) #ROTATE
select(SUBJIDN, SEX) #ROTATE
select(1:3) #ROTATE
select(SUBJIDN:CNTY) #ROTATE
select(-AGE) #ROTATE
select(-c(AGE, SEX, CNTY)) #ROTATE
select(starts_with("TRT")) #ROTATE
select(ends_with("N")) #ROTATE
select(age_yrs = AGE) #ROTATE
select(where(is.character))#ROTATE
```
---
`r chunk_reveal("dplyr_select2", widths = c(0.75,1.25), title = "### select() based on another data frame")`
```{r dplyr_select2, eval = F, echo = F}
# Base Data
mini_adsl
# Meta Data
aux_meta
# Selected Data
mini_adsl %>%
select(aux_meta %>% pull(var_name))
```
---
class: inverse, bullet
# dplyr
## filter()
- The `filter()` verb is used to subset rows
- Use logical operators to build filter criteria
- `<`, `>`, `>=`, `<=`, `&`, `|`, `==`, `!`
- Use functions to build filter criteria
- Similar to a SAS `where` and (subsetting) `if` statements
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/dplyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
`r chunk_reveal("dplyr_filter", break_type = "rotate", widths = c(1,1.5), title = "### filter()")`
```{r dplyr_filter, eval = F, echo = F}
# Base Data
mini_adsl
# Filtered Data
mini_adsl %>%
filter(SEX == "F") #ROTATE
filter(SEX == "F", AGE > 50) #ROTATE
filter(SEX == "F" | AGE > 50) #ROTATE
filter(AGE < mean(AGE)) #ROTATE
filter(year(TRTSDT) == 2019) #ROTATE
filter(CNTY %in% c("USA","CHINA")) #ROTATE
filter(!(CNTY %in% c('USA','CHINA'))) #ROTATE
filter(str_detect(CNTY, "IN")) #ROTATE
filter(substr(CNTY,1,2) == "IN") #ROTATE
filter(!is.na(CNTY)) #ROTATE
filter(AGE <= 39, AGE >= 20) #ROTATE
filter(between(AGE,20,39)) #ROTATE
```
---
class: inverse, bullet
# dplyr
## arrange() + distinct()
- The `arrange()` verb sorts column values
- `distinct()` removes duplicate rows
- Both accept single or multiple columns
- Similar to a SAS `proc sort` with (or without) a `nodupkey` option
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/dplyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
`r chunk_reveal("dplyr_arrange", break_type = "rotate", widths = c(1,1.5), title = "### arrange()")`
```{r dplyr_arrange, eval = F, echo = F}
# Base Data
adxy
# Arranged Data
adxy %>%
arrange(SUBJIDN) #ROTATE
arrange(SUBJIDN, VISITDT) #ROTATE
arrange(desc(SUBJIDN)) #ROTATE
arrange(desc(SUBJIDN), VISITDT) #ROTATE
```
---
`r chunk_reveal("dplyr_distinct", break_type = "rotate", widths = c(1.5,1.5), title = "### distinct()")`
```{r dplyr_distinct, eval = F, echo = F}
# Base Data
adxy %>%
arrange(SUBJIDN, VISITDT)
# Distinct Data
adxy %>%
arrange(SUBJIDN, VISITDT) %>%
distinct(SUBJIDN) #ROTATE
distinct(SUBJIDN, .keep_all = TRUE) #ROTATE
```
---
class: inverse, bullet
# dplyr
## mutate()
- The `mutate()` verb:
- creates new columns
- modifies existing columns
- Use logical operators and functions to accomplish this
- Similar to several SAS `data step` operations
- Better fit for some rowwise operations
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/dplyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
`r chunk_reveal("dplyr_mutate1", break_type = 'rotate', widths = c(1.5,1.5), title = "### mutate()")`
```{r dplyr_mutate1, eval = F, echo = F}
# Base Data
mini_adsl2
# Mutated Data
mini_adsl2 %>%
mutate(new_var = 1) #ROTATE
mutate(BMI = (WEIGHT / HEIGHT**2)) #ROTATE
mutate(BMI = (WEIGHT / HEIGHT**2) %>% round(2)) #ROTATE
mutate(SAFFL = if_else(SAFFN == 1, "Y", "N")) #ROTATE
mutate(SAFFL = if_else(SAFFN == 1, "Y", "N", " ")) #ROTATE
mutate(SAFFL = if_else(SAFFN == 1, "Y", "N", "M")) #ROTATE
mutate(AGE = AGE * 12) #ROTATE
```
---
`r chunk_reveal("dplyr_mutate2", break_type = 'non_seq', widths = c(1.5,1), title = "### case_when()")`
```{r dplyr_mutate2, eval = F, echo = F}
# Base Data
mini_adsl2 %>%
select(SUBJIDN, AGE)
# Mutated Data
mini_adsl2 %>%
select(SUBJIDN, AGE) %>% #BREAK
mutate(AGECAT1 = case_when(AGE >= 18 & AGE <=35 ~ "18-35", #BREAK1
AGE >= 36 & AGE <=49 ~ "36-49", #BREAK2
AGE >= 50 ~ "50+" #BREAK3
)) #BREAK
```
---
class: inverse, bullet
# dplyr
## summarize()
- `summarize()` compacts individual observations into summaries
- the output is stored in a new data frame
- similar to SAS `proc means` or `proc univarate` with `ods` out
- summaries can be column-wise or row-wise
- some row-wise operations better handled by `mutate()`
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/dplyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
`r chunk_reveal("dplyr_summarize", break_type = "rotate", widths = c(1.5,1), title = "### summarize() - columns")`
```{r dplyr_summarize, eval = F, echo = F}
# Base Data
mini_adsl3
# Summarized Data
mini_adsl3 %>%
summarise(avg_age = mean(AGE)) #ROTATE
summarise(avg_age = mean(AGE), sd_age = sd(AGE)) #ROTATE
summarise(n_age = n()) #ROTATE
summarise(across(c(DBP, SBP), mean)) #ROTATE
summarise(across(c(DBP, SBP), list(mean = mean, sd = sd))) #ROTATE
```
---
`r chunk_reveal("dplyr_summarize2", break_type = "rotate", widths = c(1.5,1), title = "### summarize() - rows")`
```{r dplyr_summarize2, eval = F, echo = F}
# Base Data
mini_adsl4
# Summarized Data
mini_adsl4 %>%
rowwise(SUBJIDN) %>%
summarise(min_qol = min(c(QOL_1, QOL_2, QOL_3))) #ROTATE
summarise(total_qol = sum(c(QOL_1, QOL_2, QOL_3))) #ROTATE
summarise(avg_qol = mean(c(QOL_1, QOL_2, QOL_3))) #ROTATE
```
---
class: inverse, bullet2
# dplyr
## group_by()
- The `group_by()` and `ungroup()` verbs enable by-group processing
- Used more as a modifier to other verbs the analysis (e.g. `summarise()`)
> data %>%
> group_by(ID) %>%
> function_1() %>%
> ungroup()
- You can combine group and non-group processing in a single pipeline
- Similar to a SAS `by` statement
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/dplyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
`r chunk_reveal("dplyr_groupby", widths = c(1.5,1.5), title = "### group_by()")`
```{r dplyr_groupby, eval = F, echo = F}
# Base Data
mini_adsl
# Calculate Avg, Overall
mini_adsl %>%
summarize(avg = mean(AGE))
# Calculate Avg, by groups
mini_adsl %>%
group_by(CNTY, TRTP) %>%
summarize(avg = mean(AGE)) %>%
ungroup() %>%
# Continue pipeline ungrouped
filter(!is.na(CNTY))
```
---
class: inverse, center, middle
# Tidying Data
---
class: inverse
# tidyr
## Tidy Data
- Helpers to transform `raw` data into a tidy format
- Tidy Format:
> Every Column is a `variable`
> Every Row is an `observation`
> Every Cell is a `single value`
<br>
- Package contains many odds and ends that fit a variety of scenarios
- Let's look at a few interesting (and common) ones
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/tidyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
class: inverse, bullet
# tidyr
## pivot_()
- The `pivot_()` family of functions reshape data
- `pivot_wider()` - long data to wide data
- `pivot_longer()` - wide data to long data
- Similar to SAS `proc transpose`
- Many additional options (e.g. prefix, suffix)
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/tidyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
`r chunk_reveal("tidyr_pivot", widths = c(1.5,1.5), title = "### pivot_wider()")`
```{r tidyr_pivot, eval = F, echo = F}
# Base Data
long_data
# Make Wide
long_data %>%
pivot_wider(id_cols = SUBJIDN,
names_from = "CYCLE",
values_from = "AVAL")
```
---
`r chunk_reveal("tidyr_pivot2", widths = c(1.5,1.5), title = "### pivot_longer()")`
```{r tidyr_pivot2, eval = F, echo = F}
# Base Data
wide_data
# Make long
wide_data %>%
pivot_longer(cols = c("Cycle1","Cycle2","Cycle3"),
names_to = "VISIT")
```
---
class: inverse , bullet
# tidyr
## fill()
- `fill()` automatically replaces missing values with a neighboring value
- Can specify direction of neighboring values
- Important to specify `group_by()` to prevent overflow
- Might be done in SAS using `retain` or `array`
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/tidyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
`r chunk_reveal("tidyr_fill", widths = c(1.5,1.5), title = "### fill()")`
```{r tidyr_fill, eval = F, echo = F}
# Base Data
fill_data
# Filled Data
fill_data %>%
fill(AVAL)
```
---
`r chunk_reveal("tidyr_fill2", break_type="rotate", widths = c(1.5,1.5), title = "### fill() + group_by()")`
```{r tidyr_fill2, eval = F, echo = F}
# Base Data
fill_data
# Filled Data
fill_data %>%
group_by(SUBJIDN) %>%
fill(AVAL) #ROTATE
fill(AVAL, .direction = "down") #ROTATE
fill(AVAL, .direction = "up" ) #ROTATE
ungroup() #ROTATE
```
---
class: inverse, bullet
# tidyr
## separate_()
- The `separate_()` family of functions splits a string into individual elements
- `separate()` splits a string into columns
- `separate_rows()` splits a string into rows
- Each requires a delimiter
- Powerful for text data, SAS metadata, novel data
- Similar to SAS `scan` function + others
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/tidyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
`r chunk_reveal("tidyr_separate", widths = c(1.5,1.5), title = "### separate_()")`
```{r tidyr_separate, eval = F, echo = F}
# Base Data
sep_data
# Separate into Columns
sep_data %>%
separate(ASR,
into = c("Age","Sex","Race"),
sep = "/")
# Separate into Rows
sep_data %>%
separate_rows(ASR,
sep = "/")
```
---
class: bullet
# Biomarker Data Example
- Novel (e.g. non-standard) data format
- <b><span style="color:#FF3162">Vendor Company</span></b> -> <b><span style="color:#00BCFF">Bioinformatician</span></b> -> <b><span style="color:#89D329">Stat Analysts</span></b>
<br>
<br>
```{r echo=FALSE, out.width="800px", fig.align='center'}
knitr::include_graphics("assets/images/bm.png")
```
<br>
- Need to `tidy` this data to be able to "use" it
---
`r chunk_reveal("tidyr_separate2", widths = c(100,100), title = "### Biomarker Data Example")`
```{r tidyr_separate2, eval = F, echo = F}
# Base Data
adbm
# Separated Data
adbm %>%
# Separate multiple mutations per subject
separate_rows(mutation,
sep=";") %>%
# Separate each mutation into it's components
separate(mutation,
sep=" ",
into=c('gene','mutation','allele')) %>%
# Convert allele to numeric for analysis
mutate(allele = allele %>% as.numeric())
```
---
class: inverse, bullet
# tidyr
## expand() + complete()
- These set of functions help enumerate combinations of variables
- `expand()` enumerates all **unique** n-way combinations of variables
- might be useful as an intermediary
- `complete()` makes missing combinations explicit
- might be useful for an actual TLF
```{r echo=FALSE}
htmltools::img(src = knitr::image_uri("assets/images/tidyr.png"),
alt = 'logo',
style = 'position:absolute; top:0; right:0; padding:30px; width: 175px; height: 175px')
```
---
`r chunk_reveal("tidyr_ec", widths = c(1.5,1.5), title = "### expand() + complete()")`
```{r tidyr_ec, eval = F, echo = F}
# Base Data
ec_data