-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8-Exercise-Solutions.Rmd
521 lines (344 loc) · 15.2 KB
/
8-Exercise-Solutions.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
\backmatter
\setcounter{chapter}{0}
\renewcommand{\thechapter}{\Alph{chapter}}
# Appendix: Exercise Solutions
```{r, echo=TRUE, message= FALSE}
require(dplyr)
```
## Tutorial 2: Introduction to R
This notebook contains the solutions for all of the **Try it yourself** sections of **tutorial 1: Introduction to R**. Make sure that you have at least tried to solve these sections first before viewing this notebook.
### 2.1 {-}
a. Can you replicate and solve these problems in R?
* 2^2
* 2 × 2
* 2 + 5 × (5 ÷ 4)^6
* what is the remainder of 52 ÷ 5
* what is the whole number solution to 82 ÷ 8
```{r}
2^2
2 * 2
2 + 5 * (5 / 4)^6
52 %/% 5
82 %% 8
```
b. Can you solve for x using R?
a <- 9 + 3 * 6
x <- a ÷ 2
```{r}
a <- 9 + 3 * 6
x <- a / 2
x
## x is equal to 13.5!
```
### 2.2 {-}
Translate the following into R and find the output:
* 8 times 3 is greater than 8?
* eleven divided by seven is not equal to 2?
* 9 is less than or equal to 18?
```{r}
8 * 3 > 8
11 / 7 != 2
9 <= 18
```
### 2.3 {-}
Can you try storing a string? Assign the string "hello world, I am here" to the variable named start.
```{r}
start <- "hello world, I am here"
start
```
**Note how the string is in "" but the variable name is not. Why do you think this is?**
Because `start` is now a known variable that stores a string. In other words, `start` does not mean "start", instead it means "hello world, I am here".
### 2.4 {-}
It is important to note that **R is case-sensitive**. This means that it distinguishes capitalized from non-capitalized characters, so logical and Logical are read as two separate things by R!
Try typing Logical with a capitalized "L". How does R respond to this?
```{r}
#Logical
### an error message that says "object 'Logical' not found" pops up
```
### 2.5 {-}
Why do you think numeric, character, and logical are not in "" but Number, Text, and T/F are?
Because numeric, character, and logical are known variables that hold meanings (and that we already defined), while Number, Text, and T/F are not. They are actually just texts that we use to rename our column names - they do not hold any meanings.
### 2.6 {-}
1. What are 2 ways that we can print rows 1 to 5 of the data frame faithful?
```{r}
## 1. print(faithful[1:12, ])
## 2. faithful[1:12, ]
```
2. What is the value of the cell in the fourth row and second column of the data frame faithful?
```{r}
## faithful[4,2]
```
### 2.7 {-}
Write a code to find the structure of the variable waiting in the faithful dataset.
```{r}
## str(faithful$waiting)
```
### 2.8 {-}
Remember those variables that we created earlier in the tutorial? Try finding the lengths of data frame and numeric.
**Challenge:** Psst! There are actually 2 ways for you to find the length of numeric.
```{r}
## length(dataframe)
## length(numeric) #OR
## length(dataframe$numeric)
```
### 2.9 {-}
Recall that in order for us to refer to a variable in a dataset, we need to first type the dataset name following by a $ before we can type the variable name.
A way to avoid repeating `faithful$` everytime is to attach the dataset using `attach(faithful)`.
Try attaching the dataset faithful then find the mean of the variable eruptions without using $!
```{r}
## attach(faithful)
## mean(eruptions)
```
## Tutorial 3: Importing Data into R with readr
This notebook contains the solutions for all of the **Try it yourself** sections of **tutorial 2: Importing Data into R with readr**. Make sure that you have at least tried to solve these sections first before viewing this notebook.
### 3.1 {-}
Can you try importing the `bpx.csv` file into R using the function `read_csv()`?
```{r}
#read_csv("../input/import/bpx.csv")
```
### 3.2 {-}
Can you identify the mistakes of the following codes?
**a.** The pathway is not in ""
**b.** "DEMO" should not be capitalized
**c.** "Read_csv" should not be capitalized
**d.** The entire pathway needs to be in "" instead of just the file name
### 3.3 {-}
Just by looking at the actual data frame, can you guess what type of data `col_double()` and `col_character()` are?
(**HINT**: doubles? integers? logical? character?)
Here is a list of `col_x()` and what they mean:
* `col_double()` – Doubles
* `col_integer()` – Integers
* `col_logical()` – True/False
* `col_date()` – Date
* `col_time()` – Time
* `col_datetime` – Date and Time
* `col_character()` – Text, Character, and everything else
This list is not extensive, so you can use `?cols` to learn more about column specification!
### 3.4 {-}
You may also notice that the header of *Skip_2* is incorrect. This is because R recognizes the header of our data as the first row, thus omiting it when importing `demo.csv` into R.
Let's say this is not what we really want. What we actually want to do is to remove the first two rows of actual data while keeping the header. What do you think we have to do to achieve this?
(**HINT**: Recall what we learn about extracting rows in tutorial 1)
```{r}
#DEMO[3:10, ]
```
### 3.5 {-}
Import the `bpx.xlsx` into R using the `read_excel()` function.
```{r}
#read_excel("../input/import/bpx.xlsx")
```
## Tutorial 4: Introduction to NHANES
This notebook contains the solutions for all of the **Try it yourself** sections of **Tutorial 3: Introduction to NHANES**. Make sure that you have at least tried to solve these sections first before viewing this notebook.
### 4.1 {-}
a. Find all the Examination Data in survey cycle 2013-2014
```{r}
## nhanesTables('EXAM', 2013)
```
b. Import the blood pressure dataset in the Examination Data in survey cycle 2013-2014
```{r}
## bpx <- nhanes('BPX_H')
```
c. Translate the following variables in the BPX dataset
* BPXPULS - Pulse regular or irregular?
* BPAARM - Arm selected
```{r}
## bpxtranslate <- nhanesTranslate('BPX_H', c('BPXPULS', 'BPAARM'), data = bpx)
```
## Tutorial 5: Data Analysis with dplyr
This notebook contains the solutions for all of the **Try it yourself** sections of **Tutorial 4: Data Analysis with dplyr**. Make sure that you have at least tried to solve these sections first before viewing this notebook.
### 5.1 {-}
In the bpx dataframe, keep the following variables and top 5 rows only:
+ SEQN
+ PEASCST1
+ PEASCTM1
+ BPXSY1
+ BPXDI1
```{r}
## new_bpx <- select(bpx,
## c(SEQN,
## PEASCST1,
## PEASCTM1,
## BPXSY1,
## BPXDI1))
## new_bpx <- head(new_bpx,5)
```
### 5.2 {-}
+ SEQN -> id
+ PEASCST1 -> bp_status
+ PEASCTM1 -> bpt_sec
+ BPXSY1 -> systolic
+ BPXDI1 -> diastolic
```{r}
## new_bpx <- rename(bpx,
## id = SEQN,
## bp_status = PEASCST1,
## bpt_sec = PEASCTM1,
## systolic = BPXSY1,
## diastolic = BPXDI1)
```
### 5.3 {-}
In the demo dataframe, find all the records that:
a. he participant who is a male
```{r}
## filter(final_demo, gender == "Male")
```
b. the participant who is a male and is older tha 50 years old
```{r}
## filter(final_demo, gender == 'Male' && age > 50)
```
c. the education level is missing
```{r}
## filter(final_demo, is.na(edu))
```
### 5.4 {-}
Re-order the rows in the bpx dataset by Blood Pressure Time in Seconds (bpt_sec) in descending order:
```{r}
## arrange(final_bpx,desc(bpt_sec))
```
### 5.5 {-}
a. Create a new variable called called **rescale_bpt_sec** that records the Blood Pressure Time in miuntes. Keep both original and new variables.
```{r}
## mutate(final_bpx,rescale_bpt_sec = bpt_sec/60)
```
b. Create **rescale_bpt_sec** in the same way above and **only keep new variables**.
Note: Try to avoid using select().
```{r}
## transmute(final_bpx,rescale_bpt_sec = bpt_sec/60)
```
### 5.6 {-}
Find the average age in the demo dataframe per education level
```{r}
## by_edu <- group_by(edu)
## summarize(by_edu, average_age = mean(age, na.rm = TRUE))
```
### 5.7 {-}
Return the observations which has more than 3 records in each gender group.
```{r}
## by_gender <-group_by(final_demo,gender)
## filter(by_edu,n()>3)
```
### 5.8 {-}
Compute the difference in each age and the average mean in each education level group
```{r}
## by_edu <-group_by(final_demo,edu)
## mutate(by_edu, diff_age = age - mean(age, na.rm = T))
```
### 5.9 {-}
Re-write the following code using pipe operator
```{r}
## temp <- filter(final_bpx, systolic > 120)
## temp <- mutate(temp, bpt_min = bpt_sec/60)
## temp
```
```{r}
## final_bpx %>%
## filter(systolic > 120) %>%
## mutate(bpt_min = bpt_sec/60)
```
## Tutorial 6: Data Visualization with ggplot2
This notebook contains the solutions for all of the **Try it yourself** sections of tutorial 5: Data Visualization with ggplot. Make sure that you have at least tried to solve these sections first before viewing this notebook.
### 6.1 {-}
Plot a scatterplot to show the relationship between Diastolic Blood Pressure (x-axis) and Blood Pressure Time in Seconds (y-axis).
```{r}
## ggplot(data = demo_bpx) +
## geom_point(aes(x = Diastolic,
## y = BPT),
## na.rm = TRUE)
```
### 6.2 {-}
Plot a scatterplot using the Diastolic (x-axis) and Systolic (y-axis) variables in the data frame demo_bpx where all of the data points are blue.
```{r}
## ggplot(demo_bpx) +
## geom_point(aes(x = Systolic,
## y = Diastolic),
## color = "blue",
## na.rm = TRUE)
```
### 6.3 {-}
Try increasing and decreasing the binwidth of a frequency polygon. What differences do you see? What does binwidth actually mean?
Higher binwidths give us less fluctuations than lower binwidths. When we are increasing the binwidths, we are actually increasing the length of intervals, which leads to less intervals. In other words, each data point are a bit further away from each other, so when connected, the graph looks less detailed and smoother.
### 6.4 {-}
Try recreating the graph above but without any missing (NA) values.
**HINT**: Filter out any information we do not need using logical operators!
First, we need to filter out all of the NA values:
```{r}
## no_NA <- filter(demo_bpx, Race != "NA" & Gender != "NA")
```
Then, we can graph our facets normally:
```{r}
## ggplot(no_NA) +
## geom_point(aes(x = Diastolic, y = Systolic), na.rm = TRUE) +
## facet_grid(Race ~ Gender)
```
## Tutorial 7: Date and Time Data with lubridate
This notebook contains the solutions for all of the **Try it yourself** sections of **tutorial 6: Date & Time Data with lubridate**. Make sure that you have at least tried to solve these sections first before viewing this notebook.
### 7.1 {-}
After running the `today()` and `now()` codes above, what do you see?
Try to also change the time zone to where you are or to something else. Now what do you see when you run `today()` and `now()`?
After running `today()` and `now()`, you should see that `today()` is a date data and `now()` is a datetime data.
Changing the time zone means that the date and time will be different if we run `today()` and `now()` again.
### 7.2 {-}
Try creating a new column named "Day_visit" that only contains information of the Year, Month, and Day columns using the Friends_visits dataframe that we just created.
```{r}
## head(
## Friends_visits %>%
## mutate(Hour_visit = make_datetime(Year, Month, Day))
## )
```
### 7.3 {-}
Using the functions introduced above, solve the following questions:
1. What day of the week is April 2, 2014?
2. What day of the year is 2017-09-15?
3. What day of the month is 20190830?
4. Find the months of the last 11 records of the column Visit_hour.
```{r}
#1. wday(mdy("April 2, 2014"), label = TRUE)
#2. yday(ymd("2017-09-05"))
#3. mday(ymd(20190830))
#4. tail(month(Friends_visits$Visit_time, label = TRUE), 11)
```
### 7.4 {-}
Using the same Friends_visit dataset, create a similar graph as above but the x-axis is days of the week. In other words, create a bar graph that shows how many visits there are in each day of the week.
```{r}
## Friends_visits %>%
## mutate(Week_day = wday(Visit_time, label = TRUE, abbr = FALSE)) %>%
## ggplot(aes(Week_day)) +
## geom_bar()
```
### 7.5 {-}
Try to update our month to 13. What happened to our date/time? What is the output?
```{r}
# DT %>%
# update(month = 13)
## The year turns to 2021 because a year only has 12 months!
```
### 7.6 {-}
Recreate the frequency polygon above but change the binwidth so that all flights within each 30 minutes are clumped into one single data point. How do the graphs differ? Can you think of a few scenarios where one would be preferred?
```{r}
## Friends_visits %>%
## mutate(Visit_hour = update(Visit_time, yday = 1)) %>%
## ggplot(aes(Visit_hour)) +
## geom_freqpoly(binwidth = 900)
```
## Tutorial 8: Data Summary with tableone
This notebook contains the solutions for all of the **Try it yourself** sections of **tutorial 7: Data Summary with tableone**. Make sure that you have at least tried to solve these sections first before viewing this notebook.
### 8.1 {-}
**Challenge**: Why do you think we need to change the names of our variables AFTER we translate them?
**Hint**: Think about the `data = demo` argument in `nhanesTranslate()`
Because after we use the function `names()` our data is no longer recognizable by R as being connected to the DEMO_H dataset that we downloaded from NHANES.
### 8.2 {-}
Create a tableone without the `vars` argument. What do you see?
Do you think the `vars` argument is necessary in our case? If not, in what situation(s) do you think it would be necessary?
The `vars` argument tells R which variables you want to include in your tableone. If it is missing from `CreateTableOne()`, this means that we want R to select **ALL** variables of the original table in our tablone. Therefore, in our case, `vars` is not neccesary because we are selecting all variables anyway. But in cases where we only want to select some variables to include in our tableone, `vars` is an absolute must!
### 8.3 {-}
How do you know if a variable is nonnormal? Try using the function `summary()` and look at the number under skew. How do you decide if something is normal or nonnormal? Is the decision to make “Age” nonnormal accurate?
After you plug the dataset name into `summary()`, you should see a numerical value below "skew" that tells us the skewness of the data. The further it is from 0 (both negative and positive), the further it is from normal! The decision to make "Age" nonnormal is arguably not quite accurate because the skewness of "Age" is only 0.4.
### 8.4 {-}
Create a tableone using the demo_translate dataset. Keep all variables and stratified the data using “Age”. What do you see? Do you think this is a helpful tableone?
```{r}
## CreateTableOne(data = demo_translate,
## vars = c("Race", "Education", "Gender"),
## factorVars = c("Race","Education", "Gender"),
## strata = "Age"
## )
```
Just like how we should not use continuous variables to create facets (check ggplot tutorial), we should also not stratified our data using continuous variables. When we stratify our data using numerical continuous variables, the tableone becomes too big to handle. The information that we get is also not meaningful as there is a lot of values including lots of NA values. If we want to stratify our data by age, it may be better to use age groups instead of single age values.