-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2022_NICAR_R_Training.Rmd
384 lines (259 loc) · 8.13 KB
/
2022_NICAR_R_Training.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
---
title: "Learning R"
author: "Lucia Walinchus"
date: "6/23/2020"
output: html_document
---
#Welcome to learning R at NICAR 2022!
Overview of the hands-on course:
*Session 1:
Jump into data analysis with R, the powerful open-source programming language. In this class we’ll cover R fundamentals and learn our way around the RStudio interface for using R.
This session is good for: People with a basic understanding of code who are ready to go beyond Excel.
*Session 2:
We'll use the tidyverse packages dplyr and ggplot2, learning how to sort, filter, group, summarize, join, and visualize to identify trends in your data. If you want to combine SQL-like analysis and charting in a single pipeline, this session is for you.
This session is good for: People who have worked with data operations in SQL or Excel and would like to do the same in R.
*Session 3:
Learn how to use R to scrape data from web pages, access APIs and transform the results into usable data. This session will also focus on how to clean and structure the data you've gathered in preparation for analysis using tidyverse packages.
This session is good for: People who have used R and have a basic understanding of how to retrieve data from APIs.
###First up: WTH am I here?
What R makes easier:
*Collaboration:
Eye on Ohio Police Stops: https://eyeonohio.com/investigation-blacks-black-neighborhoods-most-likely-to-be-traffic-stop-targets-in-ohios-3-biggest-cities/
ProPublica: https://www.propublica.org/article/minority-neighborhoods-higher-car-insurance-premiums-methodology
*You can save your script:
https://www.legislature.ohio.gov/schedules/legislative-calendar
*Very cool packages:
-Census Data
-Mapping Data
-Automatically updating data
-Modeling Data
-Dates Are Easier!
-And More
####Play around with Math
```{r}
5 + 7
5 ^7
5/7
mean(5:7)
```
####Installing a package
```{r}
install.packages("lubridate")
library(lubridate)
```
Okay let's take this puppy for a spin!
###Creating a Variable
Format:
Yo.Variable <- equation or constant
```{r}
dob <- "1999-12-31"
today()
today()-as_date(dob)
#If you want to get fancy
interval_period = interval(dob, today())
full_year = interval_period %/% years(1)
#Note: %/% is integer division. 5 %/% 2 is 2.
remaining_weeks = interval_period %% years(1) %/% weeks(1)
remaining_days = interval_period %% years(1) %% weeks(1) %/% days(1)
sprintf('Your age is %d years, %d weeks and %d days', full_year, remaining_weeks, remaining_days)
```
Loading all the other packages we need:
```{r}
library(dplyr)
library(ggplot2)
library(rio)
library(zip)
library(purrr)
library(tidyverse)
library(fs)
library(DT)
```
###Setting the working directory
Can also do Session > Set Working Directory
```{r}
#setwd(Desktop/hands_on_classes/20220303_r_1_intro_to_r_and_rstudio_2073)
```
####Importing a File
Format:
import(your file or the URL)
Can also specify rio::import
```{r}
Firefighters <- import("https://www.iaff.org/wp-content/uploads/Copy-of-FFFM20-21_470-names.xlsx")
```
###Looking through documentation
Check this out too
https://cran.r-project.org/web/packages/rio/vignettes/rio.html
How would you get this to work?
https://www.nrscotland.gov.uk/files/statistics/babies-names/20/babies-first-names-20-data.xlsx
```{r}
?read_excel
```
## Baby Names
Bringing in the government's list of baby names from the [Social Security Administration](https://www.ssa.gov/oact/babynames/limits.html)
NOTE: This is only the most popular 1,000 names per year.
```{r}
setwd("~Desktop/hands_on_classes/20220303_r_1_intro_to_r_and_rstudio_2073")
#download.file("https://www.ssa.gov/oact/babynames/names.zip", dest="dataset.zip", mode="wb")
#unzip ("dataset.zip", exdir = "./")
```
Putting all those files together and making it look nice
```{r}
setwd("~Desktop/hands_on_classes/20220303_r_1_intro_to_r_and_rstudio_2073")
#list_of_files <- list.files(path = ".", recursive = TRUE,
#pattern = "\\.txt$",
# full.names = TRUE)
#df <- list_of_files %>%
#purrr::set_names(nm = (basename(.) %>% tools::file_path_sans_ext())) %>%
#purrr::map_df(read_csv,
#col_names = FALSE,
#.id = "FileName",
#col_types = cols(X2 = col_character()))
#US_Names <- df %>%
#mutate(year = str_sub(FileName,4,8)) %>%
#select(-FileName) %>%
#mutate(name = X1, gender = X2, number = X3) %>%
#select(-X1, -X2, -X3) %>%
#distinct()
#US_Names$year <- as.numeric(US_Names$year)
```
New! Babynames package:
```{r}
install.packages("babynames")
library(babynames)
babynames <- babynames
```
Making a summary of your data:
```{r}
summary(babynames)
```
Looking at the structure
```{r}
str(babynames)
```
What is every name total?
```{r}
unique(babynames$name)
```
How many different names are there total?
Note this counts names of different sexes as two, and it doesn't count names fewer than 5.
```{r}
length(unique(babynames$name))
```
####Using DPLYR verbs
```{r}
head(babynames)
```
```{r}
babynames %>% select(-prop) %>% head()
```
```{r}
babynames %>% filter(name=="Mary") %>% head()
```
```{r}
datatable(babynames %>% filter(name=="Mary"))
```
```{r}
babynames %>%
filter(name=="Mary") %>%
summarize(sum(n))
```
```{r}
babynames <- babynames %>%
rename(number=n)
head(babynames)
```
```{r}
Babynames_By_Name <- babynames %>%
group_by(name) %>%
summarize(Total=sum(number))
```
```{r}
Babynames_By_Name_and_Year <- babynames %>%
group_by(year, name) %>%
summarize(Total=sum(number))
```
```{r}
Babynames_By_Name_and_Year <- babynames %>%
group_by(year, name) %>%
summarize(Total=sum(number)) %>%
arrange(desc(Total))
```
```{r}
Top_Names_Your_Year <- babynames %>%
filter(year==1985) %>%
group_by(name) %>%
summarize(Total=sum(number)) %>%
mutate(Top=data.table::frankv(Total, order=-1, ties.method ="first"))
```
frankv stands for "fast rank vector"
Bonus! Save your name
```{r}
Marys <- babynames %>%
filter(name=="Mary" & sex == "F")
ggplot(Marys, aes(year, number))+
geom_point()+
theme(axis.text.x = element_text(angle = 45, vjust =1.2, hjust = 1.1))+
labs(title = "Children Named 'Mary' in the United States per Year", subtitle = "Note: Data only includes top 1,000 names per year.")
ggsave("Mary.jpg", plot=last_plot())
```
Now for the state data set.
```{r}
#download.file("https://www.ssa.gov/oact/babynames/state/namesbystate.zip", destfile = "state_names_dataset.zip", mode="wb")
#unzip ("state_names_dataset.zip", exdir = "State_Names/")
```
```{r}
#setwd("~/Code/Red/BabyNames/State_Names")
#list_of_files <- list.files(path = ".", recursive = TRUE,
#pattern = "{6}",
#pattern = "\\.txt$",
#full.names = TRUE)
#list_of_files <-list_of_files[- 43]
#df <- list_of_files %>%
#purrr::set_names(nm = (basename(.) %>% tools::file_path_sans_ext())) %>%
#purrr::map_df(read_csv,
#col_names = FALSE,
# .id = "FileName",
# col_types = cols(
#X1 = col_character(),
#X2 = col_character(),
#X3 = col_double(),
#X4 = col_character(),
#X5 = col_double()
#))
```
Looking just at *Ohio*
```{r}
OH_Names <- df %>%
select(-FileName) %>%
mutate(state=X1, gender = X2, year = X3, name = X4, number = X5) %>%
select(-X1,-X2,-X3,-X4,-X5) %>%
filter(state=="OH") %>%
distinct()
```
Most popular in Ohio
```{r}
Most_Popular_OH <- OH_Names %>%
group_by(year, name, number) %>%
arrange(desc(number))
datatable(Most_Popular_OH)
```
Lots
```{r}
ManyNames <- US_Names %>%
filter(name=="Edith" |
name=="Rose"|
name=="Gina"|
name=="Tara"|
name=="Kaitlyn"|
name=="Tanisha"|
name=="Maria"|
name=="Yvonne"|
name=="Lois"& gender =="F")
ggplot(ManyNames, aes(year, number, fill=name))+
geom_bar(stat = "identity")+
facet_wrap(~name)+
theme(axis.text.x = element_text(angle = 45, vjust =1.2, hjust = 1.1))+
labs(title = "US Baby Names",subtitle = "Sample of 9 popular baby names in the US per year")
```
R Intro:
https://github.com/rtburg/NICAR2020-Intro-to-R/blob/master/Intro_to_R_and_RStudio.Rmd