-
Notifications
You must be signed in to change notification settings - Fork 3
/
01-prep.Rmd
508 lines (345 loc) · 15 KB
/
01-prep.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
# DATA PREPARTION
**Chapter Links**
* [Chapter 1 Slide Show](http://tysonbarrett.com/EDUC-6600/Slides/u00_Ch1_Intro.html#1)
**Assignment Links**
* [Unit 0 Assingment - Write Up Skeleton](https://usu.box.com/s/dmj8d2u28q2ynx15v53l9f8nr1rudu8y)
* [Unit 0 Assingment - R Skeleton .Rmd](https://usu.box.com/s/di5zsmcpalactd2h08nx7p8nnm45en62)
* [Inho's Dataset - Excel format](https://usu.box.com/s/hyky7eb24l6vvzj2xboedhcx1xolrpw1)
```{r global_options, include=FALSE}
# set global chunk options...
# this changes the defaults so you don't have to repeat yourself
knitr::opts_chunk$set(comment = NA,
cache = TRUE,
echo = TRUE,
warning = FALSE,
message = FALSE)
```
---------------------------------------
## Preparing the Environment
![](img/headers/library.png)
### The `library()` Function: Load a Package
You will need TWO packages:
* `tidyverse` Easily Install and Load the 'Tidy universe' of packages [@R-tidyverse]
* `readxl` Read Excel Files [@R-readxl]
* `furniture` Nice Tables and Row-wise functions (by Tyson) [@R-furniture]
> Make sure the packages are **installed** *(Package tab)*
The function `library()` checks the package out, or makes it active.
```{r, comment=NA}
library(tidyverse) # Loads several very helpful 'tidy' packages
library(readxl) # Read in Excel datasets
library(furniture) # Nice tables (by our own Tyson Barrett)
```
### The `readxl::read_excel()` Function: Read in Excel Data Files
> Make sure the **dataset** is saved in the same *folder* as this file
> Make sure the that *folder* is the **working directory**
* Now we are ready to open the data with the `read_excel()` function from the `readxl` package
+ `readxl::read_excel()` the double colon specifies the `package::function()`
+ the only thing required inside the `()` is the quoted name of the Excel file
- Make sure it is stored in the .Rmd's folder
- Make sure to include the file's extension *(.xls)*
> NOTE: a `tibble` is basically just a "table" of data, the way the tidy-verse represents data sets.
```{r, eval=FALSE}
read_excel("Ihno_dataset.xls")
```
```{r, include=FALSE}
read_excel("data/Ihno_dataset.xls")
```
------------------------------------
## Opperators and Helpful Functions
### The Assignment Opperator `<-`: Save things to a name
* the `<-` combination of symbols makes **assignments**
+ tells **R** to store the dataset as the name it points to
+ this lets us use the dataset later on in another step
> NOTE: no output is produced when you make an assignment.
```{r, eval=FALSE}
data <- read_excel("Ihno_dataset.xls")
```
```{r, include=FALSE}
data <- read_excel("data/Ihno_dataset.xls")
```
* Print out the dataset by just typing and running the name you assigned it
```{r}
data
```
> NOTE: The pound or hashtag symbol at the front of a line within an R code chunk designates what follows as a comment and does not try to run the code.
```{r}
#data
```
### The Pipe `%>%` Opperator: Link Steps Togehter
This special set of symbols *(no spaces included)* signals R to feed what precedes it **into** what follows it. Its a simple idea that makes code writing in R much easier [@R-dplyr].
```{r, eval=FALSE}
data <- read_excel("Ihno_dataset.xls") %>%
dplyr::rename_all(tolower) # convert variable names to lower case
data
```
```{r, include=FALSE}
data <- read_excel("data/Ihno_dataset.xls") %>%
dplyr::rename_all(tolower) # convert variable names to lower case
data
```
### The `head()` Function: Print the First Few Rows
See the first few rows of a dataset by using the `head()` function. Since it is part for **base R**, I never include the package, but if we did, it would be `utils::head()`. The default is to print the first SIX rows.
```{r}
head(data)
```
Inside the `head()` function, you can change the default of `n = 6` rows. You can learn about this and other options in the **Help** tab *search for 'head'*.
```{r}
utils::head(data, n = 3)
```
```{r}
head(data, n = 11)
```
### The `names()` Function: List the Variable Names
Another helpful function is `names()` which lists out the names of the **variables**. This is nice to use to copy-paste later on, since...in R code chunks:
* Spelling matters
* Capitalization matters
* Spacing does NOT matter: one space is the same as 100 spaces
* Line enters are ignored
```{r}
names(data)
```
### The `dim()` Function: List the Dimentions
See how many rows *(observation)* and columns *(variables)*
```{r}
dim(data)
```
### The `tibble::glimpse()` Function: Gives an Overview of Variables
This is a handy function that gives [@R-tibble]:
* Dimensions (observations and variables)
* Names of variables
* Each variables type, which could be...
+ `dbl` = numeric: double precision floating point numbers
+ `fct` = factor: categorical, either nominal or ordinal
+ `chr` = character: text
* Lists the first few entries
```{r}
tibble::glimpse(data)
```
### The `dplyr::select()` Function: Specify VARIABLES to include/keep
This function chooses which **variables** to include, excluding all others not given between the `()`.
```{r}
data %>%
dplyr::select(sub_num, gender, major, reason, exp_cond, coffee)
```
### The `dplyr::filter()` Function: Specify OBSERVATIONS to include/keep
This function chooses which **observations** to include, excluding all others not given between the `()`.
```{r}
data %>%
dplyr::filter(gender == 1)
```
You can combine steps to multiple things. The steps are completed in the order we read: top to bottom, left to right.
```{r}
data %>%
dplyr::select(sub_num, gender, major, reason, exp_cond, coffee) %>%
dplyr::filter(gender == 1)
```
-----------------------------------------
## Data Wrangling
### The `dplyr::mutate()` Function: Create a New Variable
Just like radiation may cause a fish to grow an additional eye (a mutation), the `mutate()` function grows a new variable.
```{r}
data %>%
dplyr::mutate(test = 1) %>%
dplyr::select(sub_num, gender, mathquiz, statquiz, test) %>%
head()
```
### The `factor()` Function: Define Categorical Variables
We will be providing this function with three pieces of information:
(@) The name of an existing variable
(@) A *concatinated* set of numerical `levels`
(@) A *concatinated* set of textual `labels`
> You must include the SAME number of levels and labels. The ORDER in the sets designates how the labels will be applied to the levels.
Here is how it looks to create ONE new factor. Notice I added the letter `F` to designate that this new variable is a factor.
```{r}
data %>%
dplyr::mutate(genderF = factor(gender,
levels = c(1, 2),
labels = c("Female", "Male")))
```
Notice that the dataset the is printed includes our new variable at the **END**.
You can use the `pipe` to chain several mutate steps together. I have also assigned the resulting dataset with the five new factor variables at the end to a new name, `dataF`. Since this code chunk includes a single assignment, there is no output created.
> Remember to include a PIPE between all your steps, but not at the end!
```{r}
dataF <- data %>%
dplyr::mutate(genderF = factor(gender,
levels = c(1, 2),
labels = c("Female",
"Male"))) %>%
dplyr::mutate(majorF = factor(major,
levels = c(1, 2, 3, 4,5),
labels = c("Psychology",
"Premed",
"Biology",
"Sociology",
"Economics"))) %>%
dplyr::mutate(reasonF = factor(reason,
levels = c(1, 2, 3),
labels = c("Program requirement",
"Personal interest",
"Advisor recommendation"))) %>%
dplyr::mutate(exp_condF = factor(exp_cond,
levels = c(1, 2, 3, 4),
labels = c("Easy",
"Moderate",
"Difficult",
"Impossible"))) %>%
dplyr::mutate(coffeeF = factor(coffee,
levels = c(0, 1),
labels = c("Not a regular coffee drinker",
"Regularly drinks coffee")))
```
See how the new variables are added at the end.
```{r}
tibble::glimpse(data)
tibble::glimpse(dataF)
```
--------------------------------
The following portion works through the assignment for Unit 0
### Question 2: Create a new variable = `mathquiz` + 50
```{r}
data2 <- dataF %>%
dplyr::mutate(mathquiz_p50 = mathquiz + 50)
```
```{r}
data2 %>%
dplyr::select(sub_num, mathquiz, mathquiz_p50)
```
### Question 3: Create a new variable = `Hr_base` / 60
```{r}
data3 <- data2 %>%
dplyr::mutate(hr_base_bps = hr_base / 60)
```
```{r}
data3 %>%
dplyr::select(sub_num, hr_base, hr_base_bps)
```
### Question 4a: Create a new variable = `Statquiz` + 2, then * 10
```{r}
data4a <- data3 %>%
dplyr::mutate(statquiz_4a = (statquiz + 2) * 10 )
```
### Question 4b: Create a new variable = `Statquiz` * 10, then + 2
```{r}
data4b <- data4a %>%
dplyr::mutate(statquiz_4b = (statquiz * 10) + 2 )
```
```{r}
data4b %>%
dplyr::select(sub_num, statquiz, statquiz_4a, statquiz_4b)
```
### Question 5a: Create a new variable = `sum` of the 3 anxiety measures
Here are three ways you may try to find the sum. The middle way does not perform the action we want. The first way works fine, unless there is some missing data.
```{r}
data5a <- data4b %>%
dplyr::mutate(anx_plus = anx_base + anx_pre + anx_post) %>% # works, missing??
dplyr::mutate(anx_sum = sum(anx_base, anx_pre, anx_post)) %>% # does NOT work
dplyr::mutate(anx_rowsums = rowsums(anx_base, anx_pre, anx_post)) # best way
```
```{r}
data5a %>%
dplyr::select(sub_num,
anx_base, anx_pre, anx_post,
anx_plus, anx_sum, anx_rowsums)
```
\clearpage
### Question 5b: Create a new variable = `average` of the 3 heart rates
```{r}
data5b <- data5a %>%
dplyr::mutate(hr_avg = (hr_base + hr_pre + hr_post)/3) %>% # works,no missings
dplyr::mutate(hr_rowmeans = rowmeans(hr_base, hr_pre, hr_post)) # always works
```
```{r}
data5b %>%
dplyr::select(sub_num,
hr_base, hr_pre, hr_post,
hr_avg, hr_rowmeans)
```
\clearpage
### Question 6: Create a new variable = `Statquiz` minus `Exp_sqz`
```{r}
data6 <- data5b %>%
dplyr::mutate(statDiff = statquiz - exp_sqz)
```
```{r}
data6 %>%
dplyr::select(sub_num, exp_cond, exp_condF,
statquiz, exp_sqz, statDiff)
```
\clearpage
### Putting it all together
```{r, eval=FALSE}
data_clean <- read_excel("Ihno_dataset.xls") %>%
dplyr::rename_all(tolower) %>%
dplyr::mutate(genderF = factor(gender,
levels = c(1, 2),
labels = c("Female",
"Male"))) %>%
dplyr::mutate(majorF = factor(major,
levels = c(1, 2, 3, 4,5),
labels = c("Psychology",
"Premed",
"Biology",
"Sociology",
"Economics"))) %>%
dplyr::mutate(reasonF = factor(reason,
levels = c(1, 2, 3),
labels = c("Program requirement",
"Personal interest",
"Advisor recommendation"))) %>%
dplyr::mutate(exp_condF = factor(exp_cond,
levels = c(1, 2, 3, 4),
labels = c("Easy",
"Moderate",
"Difficult",
"Impossible"))) %>%
dplyr::mutate(coffeeF = factor(coffee,
levels = c(0, 1),
labels = c("Not a regular coffee drinker",
"Regularly drinks coffee"))) %>%
dplyr::mutate(mathquiz_p50 = mathquiz + 50) %>%
dplyr::mutate(hr_base_bps = hr_base / 60) %>%
dplyr::mutate(statquiz_4a = (statquiz + 2) * 10 ) %>%
dplyr::mutate(statquiz_4b = (statquiz * 10) + 2 ) %>%
dplyr::mutate(anx_sum = rowsums(anx_base, anx_pre, anx_post)) %>%
dplyr::mutate(hr_mean = rowmeans(hr_base + hr_pre + hr_post)) %>%
dplyr::mutate(statDiff = statquiz - exp_sqz)
tibble::glimpse(data_clean)
```
```{r, include=FALSE}
data_clean <- read_excel("data/Ihno_dataset.xls") %>%
dplyr::rename_all(tolower) %>%
dplyr::mutate(genderF = factor(gender,
levels = c(1, 2),
labels = c("Female",
"Male"))) %>%
dplyr::mutate(majorF = factor(major,
levels = c(1, 2, 3, 4,5),
labels = c("Psychology",
"Premed",
"Biology",
"Sociology",
"Economics"))) %>%
dplyr::mutate(reasonF = factor(reason,
levels = c(1, 2, 3),
labels = c("Program requirement",
"Personal interest",
"Advisor recommendation"))) %>%
dplyr::mutate(exp_condF = factor(exp_cond,
levels = c(1, 2, 3, 4),
labels = c("Easy",
"Moderate",
"Difficult",
"Impossible"))) %>%
dplyr::mutate(coffeeF = factor(coffee,
levels = c(0, 1),
labels = c("Not a regular coffee drinker",
"Regularly drinks coffee"))) %>%
dplyr::mutate(mathquiz_p50 = mathquiz + 50) %>%
dplyr::mutate(hr_base_bps = hr_base / 60) %>%
dplyr::mutate(statquiz_4a = (statquiz + 2) * 10 ) %>%
dplyr::mutate(statquiz_4b = (statquiz * 10) + 2 ) %>%
dplyr::mutate(anx_sum = rowsums(anx_base, anx_pre, anx_post)) %>%
dplyr::mutate(hr_mean = rowmeans(hr_base + hr_pre + hr_post)) %>%
dplyr::mutate(statDiff = statquiz - exp_sqz)
tibble::glimpse(data_clean)
```