-
Notifications
You must be signed in to change notification settings - Fork 3
/
01-notes.Rmd
482 lines (326 loc) · 10.2 KB
/
01-notes.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
---
layout: topic
title: Intro to R (notes)
author: Data Carpentry contributors
minutes: 45
---
```{r, echo=FALSE}
knitr::opts_chunk$set(results='hide', fig.path='img/r-lesson-')
```
## R and RStudio
### R
- Free and open source general statistical programming language
- Broad statistical and graphics functions
- Huge community of add-on packages
- Not just for statistics, but also general purpose programming.
### RStudio
- Integrated Development Environment (IDE) for R
- Separate program, developed by a company but also free and open
source.
- By far the best thing to use when learning R.
### RStudio orientation
Four panes (can be re-organized, but always these four panes)
- Scripts (top-left)
- R console (bottom-left)
- Environment/history (top-right)
- Files/plots/help/etc (bottom-right)
## R as a calculator
- `Ctrl-Shift-2` to focus only on the R console.
- `Ctrl-Shift-0` to get back.
- Explain `>` prompt and do some calculations
```{r add, results="markup"}
2016 - 1969
```
```{r calculations, eval=FALSE}
4*6
4/6
4^6
log(4)
log10(4)
```
- Explain that `log` is a function with two arguments.
## Getting help
- Pause for a moment after typing `log`
- Type `?log`
## Need for scripts
- Want to save what we're doing
- Really want to organize the scripts and data for a project
### Create a new R project
- File → New Project → Existing directory → Browse for folder
→ Create project
### Create an R script
- File → New File → R script
- Save it in the `Code/` directory, `data-carpentry-script.R`
## Typing into the script and pasting to console
- Type some stuff into the script
```{r rscript}
# R intro
2016 - 1969
```
- Paste to console with <kbd>`Ctrl`</kbd> + <kbd>`Enter`</kbd>
- Explain comment sign, `#`
## Assigment operator
```{r assign}
age <- 2016 - 1969
```
- Paste into console.
- Can also use `=`, but recommend `<-`.
- Can use <kbd>Alt</kbd> + <kbd>-</kbd>
### Using the stuff you assigned
```{r sqrt}
sqrt(age)
```
```{r more_assign}
sqrt_age <- sqrt(age)
round(sqrt_age)
round(sqrt_age, 2)
```
### Object names
- explicit but not too long
- can't start with a number
- there are some names not allowed (`if`, `for`, `else`)
- other names you can but shouldn't use (`c`, `data`, `mean`)
- Can use `.`, but best not to
- Reminder: The last round shows a function with 2 arguments and order matters.
### Named arguments
- Named arguments useful for commands that have lots of them as order can get confusing
- Can then switch them
- Generally the first argument or two will be entered positionally and then later ones w/name
- Names can be found using help methods from earlier
```{r round_w_named_args}
round(x = sqrt_age, digits = 2)
round(digits = 2,x = sqrt_age)
round(sqrt_age, digits = 2)
```
- We may occasionally used named arguments in these lessons
### Challenge
What is the value of `y` after doing the following?
```{r}
x <- 50
y <- x * 2
x <- 80
```
- Objects don't get linked together
## Plunge into Survey Data
```{r read_csv_from_web}
surveys <- read.csv("http://kbroman.org/datacarp/portal_data_joined.csv")
```
### Objects in workspace
- `ls()`
- Environment panel
## Data frames
Use `head()` to view the first few rows.
```{r head}
head(surveys)
```
Use `tail()` to view the last few rows.
```{r tail}
tail(surveys)
```
Use `str()` to look at the structure of the data.
```{r str}
str(surveys)
```
Talk about integers and factors; more on factors later.
### Challenge
Study the output of `str(surveys)`. How are the missing values being treated?
<!-- end challenge -->
### Another summary
```{r summary}
summary(surveys)
```
- six-number summary for the numeric columns
- table for the factor columns
## Inspecting data frames
- `dim()`
- `nrow()`
- `ncol()`
- `names()`
- `colnames()`
- `rownames()`
## Download and then load the data
We first download the file:
```{r download_file, eval=FALSE}
download.file("http://kbroman.org/datacarp/portal_data_joined.csv",
"CleanData/portal_data_joined.csv")
```
We then load them into R:
```{r load_from_local_file, eval=FALSE}
surveys <- read.csv("CleanData/portal_data_joined.csv")
```
## Indexing, Sequences, and Subsetting
We can pull out parts of a data frame using square brackets. We need
to provide two values: row and column, with a comma between them.
For example, to get the element in the 1st row, 1st column:
```{r one_one_element}
surveys[1,1]
```
To get the element in the 2nd row, 7th column:
```{r two_seven_element}
surveys[2,7]
```
To get the entire 2nd row, leave the column part blank:
```{r second_row}
surveys[2,]
```
And to get the entire 7th column, leave the row part blank:
```{r seventh_column}
sex <- surveys[,7]
```
You can also refer to columns by name, in multiple ways.
```{r grab_sex, eval = FALSE}
sex <- surveys[, "sex"]
sex <- surveys$sex
sex <- surveys[["sex"]]
```
When we pull out a single column, the result is a "vector". We can
again use square brackets to pull out individual values, but providing
a single number.
```{r grab_individual_sexes}
sex[1]
sex[10000]
```
### Slices
You can pull out larger slices from the vector by providing vectors of
indices. You can use the `c()` function to create a vector.
```{r grab_some_sexes}
c(1,3,5)
sex[c(1,3,5)]
```
To pull out larger slices, it's
helpful to have ways of creating sequences of numbers.
First, the operator `:` gives you a sequence of consecutive values.
```{r}
1:10
10:1
5:8
```
`seq` is more flexible.
```{r}
seq(1, 10, by=2)
seq(5, 10, length.out=3)
seq(50, by=5, length.out=10)
seq(1, 8, by=3) # sequence stops to stay below upper limit
seq(10, 2, by=-2) # can also go backwards
```
To get slices of our data frame, we can include a vector for the row
or column indexes (or both)
```{r}
surveys[1:3, 7] # first three elements in the 7th column
surveys[1, 1:3] # first three columns in the first row
surveys[2:4, 6:7] # rows 2-4, columns 6-7
```
### Challenge
The function `nrow()` on a `data.frame` returns the number of rows.
Use `nrow()`, in conjuction with `seq()` to create a new `data.frame` called
`surveys_by_10` that includes every 10th row of the survey data frame
starting at row 10 (10, 20, 30, ...)
<!-- end challenge -->
## Missing data
Missing data value, `NA`. Different from character string `"NA"`.
```{r create_vec_with_missing}
heights <- c(2, 4, 4, NA, 6)
```
Many functions will return `NA` if there is any missing data. Need to
use `na.rm=TRUE` to strip off missing values.
```{r na_rm}
mean(heights)
max(heights)
mean(heights, na.rm = TRUE)
max(heights, na.rm = TRUE)
```
If your data include missing values, you may want to become familiar with the
functions `is.na()` and `na.omit()`.
```{r extract_non_missing}
## Extract those elements which are not missing values.
heights[!is.na(heights)]
## shortcut to that
na.omit(heights)
```
### Treating blanks as missing
- blank character strings might be best treated as missing values.
- I prefer not to have blank fields, but it's difficult to choose a
universal missing value code.
- Use `na.strings` in `read.csv()` to indicate missing value codes.
```{r read.csv_blanks_missing, eval=FALSE}
surveys_noblanks <- read.csv("CleanData/portal_data_joined.csv", na.strings="")
```
## Factors
Factors represent categorical data. They can be useful, but they can
be a pain. And understanding them is necessary for statistical analysis and for
plotting.
Factors are stored as integers, and have labels associated with these unique
integers. While factors look (and often behave) like character vectors, they are
actually integers under the hood, and you need to be careful when treating them
like strings.
Once created, factors can only contain a pre-defined set of values, known as
*levels*. By default, R always sorts *levels* in alphabetical order. For
instance, if you use `factor()` to create a factor with 2 levels:
```{r create_sex_vector, purl=TRUE}
sex <- factor(c("male", "female", "female", "male"))
```
R will assign `1` to the level `"female"` and `2` to the level `"male"` (because
`f` comes before `m`, even though the first element in this vector is
`"male"`). You can check this by using the function `levels()`, and check the
number of levels using `nlevels()`:
```{r levels_of_sex}
levels(sex)
nlevels(sex)
```
Sometimes, the order of the factors does not matter, other times you might want
to specify a particular order.
```{r food_factor, purl=TRUE}
food <- factor(c("low", "high", "medium", "high", "low", "medium", "high"))
levels(food)
food <- factor(food, levels=c("low", "medium", "high"))
levels(food)
```
### Converting factors
If you need to convert a factor to a character vector, you use
`as.character(x)`.
Converting factors where the levels appear as numbers (such as concentration
levels) to a numeric vector is a little trickier. One method is to convert
factors to characters and then numbers.
function. Compare:
```{r convert_factor, purl=TRUE}
f <- factor(c(1, 5, 10, 2))
as.numeric(f) ## wrong! and there is no warning...
as.numeric(as.character(f)) ## works...
```
### Challenge
The function `table()` tabulates observations.
```{r table, eval=FALSE}
expt <- c("treat1", "treat2", "treat1", "treat3", "treat1",
"control", "treat1", "treat2", "treat3")
expt <- factor(expt)
table(expt)
```
* In which order are the treatments listed?
* How can you recreate this table with "`control`" listed last instead
of first?
<!-- end challenge -->
<!---
```{r correct-order}
## Answers
##
## * The treatments are listed in alphabetical order because they are factors.
## * By redefining the order of the levels
expt <- c("treat1", "treat2", "treat1", "treat3", "treat1",
"control", "treat1", "treat2", "treat3")
expt <- factor(expt, levels=c("treat1", "treat2", "treat3", "control"))
table(expt)
```
--->
### stringsAsFactors
The default when reading in data with `read.csv()`, columns with text
get turned into factors.
You can avoid this with the argument `stringsAsFactors=FALSE`.
```{r read_csv_stringsAsFactors, eval=FALSE}
surveys_chr <- read.csv("CleanData/portal_data_joined.csv", stringsAsFactors=FALSE)
```
Then when you look at the result of `str()`, you'll see that the
previously factor columns are now `chr`.
```{r str_on_that, eval=FALSE}
str(surveys_chr)
```
<br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/>