-
Notifications
You must be signed in to change notification settings - Fork 1
/
rladies-data-import-workshop.Rmd
321 lines (224 loc) · 6.17 KB
/
rladies-data-import-workshop.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
---
title: "R-Ladies RTP Getting Data into R workshop"
author: "Elaine McVey"
date: "November 14, 2016"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Welcome to R-Ladies RTP!
# Welcome to TransLoc!
## Outline
- Data sources
- Controlling the source
- Data validation and cleanup
- Understanding read_csv
- R and databases
- Problem solving
## Materials
- All source code at https://github.com/rladies/rtp-data-import-11-13-16
<!-- ## Datasets -->
<!-- - Datasets for today are here: https://www.dropbox.com/s/zhmn02ti0ggxdj7/rladies_ggplot2_datasets.rda?dl=1 -->
<!-- - You can download them from R: -->
<!-- `download.file( -->
<!-- 'https://www.dropbox.com/s/zhmn02ti0ggxdj7/ -->
<!-- rladies_ggplot2_datasets.rda?dl=1', -->
<!-- 'rladies_ggplot2_datasets.rda')` -->
<!-- `attach('rladies_ggplot2_datasets.rda')` -->
## (Some) Data Sources {.smaller}
Source Package
------ -------
flat files (csv, txt) readr
databases DBI/dplyr/DB-specifics
JSON/XML jsonlite/xml2
APIs httr
Web scraping rvest
Domain-specific from ROpenSci/Bioconductor
Stats formats (SAS, SPSS, Stata) haven
Matlab R.matlab
Excel readxl/*jailbreakr*
Google Sheets googlesheets
PDFs *tabulizer* (Tabula)
## Controlling the Source
**People wrangling over data wrangling!**
## Suggestions
- HAVE REQUIREMENTS
- naming conventions
- require tidy format
- data validation tools (spreadsheets)
- data testing upon import
## Professionalizing Data Import
**Meet the Data Engineer...**
## Data Validation and Cleanup
**NEVER ASSUME**
## Suggestions
- Inspect all variable types
- Inspect contents - variable by variable and cross-tabs (table, glimpse, etc.)
- Check number of rows
- Look for missing values
- Build checks into the code
- If the data can change, consider testing! (testthat)
## R and Databases
R can connect to many databases - and with dplyr, no SQL!!
https://cran.r-project.org/web/packages/dplyr/vignettes/databases.html
## readr (flat files)
readr handles much more than csv
_readr vignettes_
https://github.com/tidyverse/readr/blob/master/vignettes/readr.Rmd
https://cran.r-project.org/web/packages/readr/vignettes/column-types.html
## Loading readr
```{r}
library(readr)
library(tidyverse)
```
## Digging into read_csv
_R for Data Science_
http://r4ds.had.co.nz/data-import.html
## read_csv
flat file > tibble
## But what about read.csv?
- faster
- better defaults
- reproducible across OS
## read_csv basics
In the ideal case:
```
library(readr) # or library(tidyverse)
my_df <- read_csv('my_file.csv')
```
But...
## What read_csv does
1. Makes a rectangle of strings
2. Determines column types
3. Parses the columns into types
## Guessing
```read_csv``` tries to guess what the column types are using the first 1000 rows
If it has trouble, it will tell you
## Parsing columns (vectors)
- ```parse_logical()``` (simple)
- ```parse_integer()``` (simple)
- ```parse_double()``` (strict)
- ```parse_number()``` (flexible)
- ```parse_character()``` (encodings)
- ```parse_factor()``` (hi statisticians)
- ```parse_datetime()``` (ugh)
- ```parse_date()``` (ugh)
- ```parse_time()``` (ugh)
## Let's try it!
```{r}
mtcars_df <- read_csv(readr_example('mtcars.csv'))
```
## col_types
Specify column types:
- with a string of single characters: ```col_types = "dc__d"```
- with ```cols()```, set by column name
- Or, ```read_csv``` guesses
## Best practice
```{r}
mtcars_df <- read_csv(readr_example('mtcars.csv'),
col_types = cols(
mpg = col_double(),
cyl = col_integer(),
disp = col_double(),
hp = col_integer(),
drat = col_double(),
wt = col_double(),
qsec = col_double(),
vs = col_integer(),
am = col_integer(),
gear = col_integer(),
carb = col_integer()
))
```
## Let's try something tougher
```{r}
challenge <- read_csv(readr_example("challenge.csv"))
```
## Using problems()
```{r}
problems(challenge)
```
## Work through problems one column at a time...
```{r}
challenge <- read_csv(
readr_example("challenge.csv"),
col_types = cols(
x = col_integer(),
y = col_character()
)
)
```
## Manually change col_types
```{r}
challenge <- read_csv(
readr_example("challenge.csv"),
col_types = cols(
x = col_double(),
y = col_character()
)
)
```
## Now how are we doing?
```{r}
head(challenge)
```
Hmmmm....
## Now how are we doing?
```{r}
tail(challenge)
```
## Set date type
```{r}
challenge <- read_csv(
readr_example("challenge.csv"),
col_types = cols(
x = col_double(),
y = col_date()
)
)
```
## Success :)
```{r}
tail(challenge)
```
## Other strategies for solving problems
Increase guess information:
```
challenge2 <- read_csv(readr_example("challenge.csv"), guess_max = 1001)
```
Read everything in as character and work from there:
```
challenge2 <- read_csv(readr_example("challenge.csv"),
col_types = cols(.default = col_character())
)
```
## read_csv Arguments
```
read_csv(file,
col_names = TRUE,
col_types = NULL,
locale = default_locale(),
na = c("", "NA"),
quoted_na = TRUE,
comment = "",
trim_ws = TRUE,
skip = 0,
n_max = Inf,
guess_max = min(1000, n_max),
progress = interactive()
)
```
## Problem Solving
What are your data import challenges?
## Vote!
2017 meetup topics?
Format suggestions?
Come present!!
## Next Meetup
- Tuesday, December 13th
- Same location - TransLoc
- RMarkdown!
## Networking
Serena's
5311 S Miami Blvd