/
chapter5.R
183 lines (111 loc) · 2.97 KB
/
chapter5.R
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
---
title: "Chapter 5"
tutorial:
id: "code.r-journalism/chapter-5"
version: .8
output:
learnr::tutorial:
progressive: true
theme: lumen
highlight: espresso
include:
before_body: _navbar.html
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
library(learnr)
library(tidyverse)
library(checkr)
knitr::opts_chunk$set(echo = FALSE)
tutorial_options(exercise.checker = checkr::check_for_learnr)
#knitr::opts_chunk$set(exercise.checker = checker)
```
## Static maps
### Map a shapefile
### Join it to data
### Map out the joined data
### Facet maps
### Map out locations as circles
## Interactive maps
### Find the latitude and longitude of an address
### Map that out in an interactive map
### Points in a polygon join
### Map out joined data as interactive choropleth
## Intro to R
### Objects
Assign the number 17 to the object **ub**
```{r object-check, exercise=T, exercise.timelimit=60}
ub 17
ub
```
### Array
Create an array of numbers: 301, 978, and 101.
Assign it to the object "years"
```{r arrays, exercise=T, exercise.timelimit=60}
years #replace this with your code
years
```
### Functions
```{r years_array, include=FALSE}
years <- c(301, 978, 101)
```
What's the average of the array of numbers assigned to "years"?
```{r average, exercise=T, exercise.timelimit=60}
(years)
```
### Classes
```{r factors_df, include=FALSE}
burgers <- data.frame(id=c(60006,60007,60008,60009, 60010), name=c("Bob", "Linda", "Louise", "Tina", "Gene"), age=c(45, 44, 12, 13, 11), shirt=c("White", "Red", "Pink", "Blue", "Yellow"))
burgers$shirt<- factor(burgers$shirt)
burgers$id <- factor(burgers$id)
burgers$name <- as.character(burgers$name)
```
Take a look at the structure of **burgers**:
```{r structure, exercise=T, exercise.timelimit=60}
```
```{r first_quiz}
quiz(
question("What kind of class is the variable id?",
answer("character"),
answer("number"),
answer("factor", correct = TRUE),
answer("date"),
random_answer_order= TRUE
))
```
## Data structures in R
### Pulling a column of data
Consider this data frame **burgers**
```{r burger_show}
burgers
```
How do you refer to the the *shirt* variable/column with []?
```{r variable1, exercise=T, exercise.timelimit=60}
# Add to the line below
burgers
```
How do you refer to the the *shirt* variable/column with $?
```{r variable2, exercise=T, exercise.timelimit=60}
# Add to the line below
burgers
```
### Pulling a row of data
Extract entire row for Linda using [].
```{r variable4, exercise=T, exercise.timelimit=60}
# Add to the line below
burgers
```
### Converting data classes
Convert the *id* variable of the **burgers** data frame to numeric.
```{r variable3, exercise=T, exercise.timelimit=60}
# Add to the line below
burgers
```
### Boolean logic
Check if Gene's age is 11.
*Note:* Is the answer the same as above (correct) or is it 1-5 (false)?
```{r boolean, exercise=T, exercise.timelimit=60}
# Modify the line of code below
age_test <- burgers$age[5] 11
age_test
```