-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintro.Rmd
289 lines (213 loc) · 9.55 KB
/
intro.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
## Newer approaches to data wrangling
Let's say we want to select the following from our data:
- Start with the **ID** variable
- The variables **X1:X10**, which are not all together, not the only X* variables
- The variables **var1** and **var2**, which are the only *var* variables in the data
- Any variable that starts with **XYZ**
How might we go about this?
## Some base R approaches
Typically tedious
Multiple steps just to get the columns you want
```{r baseRexample1, eval=FALSE}
# numeric indexes; not conducive to readibility or reproducibility
newData = oldData[,c(1,2,3,4, etc.)]
# explicitly by name; fine if only a handful; not pretty
newData = oldData[,c('ID','X1', 'X2', etc.)]
# two step with grep; regex difficult to read/understand
cols = c('ID', paste0('X', 1:10), 'var1', 'var2', grep(colnames(oldData), '^XYZ', value=T))
newData = oldData[,cols]
# or via subset
newData = subset(oldData, select = cols)
```
## More
What if you also want observations where **Z** is **Yes**, Q is **No**
ordered by **var1** (descending)...
and only the last 50 of those results?
```{r baseRexample2, eval=FALSE}
# three operations and overwriting or creating new objects if we want clarity
newData = newData[oldData$Z == 'Yes' & oldData$Q == 'No',]
newData = tail(newData, 50)
newData = newdata[order(newdata$var1, decreasing=T),]
```
And this is for fairly straightforward operations.
## An alternative
```{r pipeExample, eval=FALSE}
newData = oldData %>%
filter(Z == 'Yes', Q == 'No') %>%
select(num_range('X', 1:10), contains('var'), starts_with('XYZ')) %>%
arrange(desc(var1)) %>%
tail(50)
```
## An alternative
Piping is an *alternative*
You can do all this sort of stuff with base R
- <span class="func">with</span>, <span class="func">within</span>, <span class="func">subset</span>, <span class="func">$</span>, etc.
While the base R approach can be concise, it is potentially:
>- noisier
>- less legible
>- less amenable to additional data changes
>- requires esoteric knowledge (e.g. regular expressions)
>- often requires new objects (even if we just want to explore)
## Piping
<span class='pipe'>%>%</span> : Passes the prior object to the function after the pipe
- x <span class='pipe'>%>%</span> f same as f(x)
```{r eval=FALSE}
object %>% function(object)
```
## Piping
<img src="img/pipeExplained.png" style="display:block; margin: 0 auto;">
## Another example...
## Start with a string, end with a map
```{r wikileafletNoEval, eval=FALSE}
wikiURL = 'https://en.wikipedia.org/wiki/List_of_United_States_cities_by_population'
# Let's go!
wikiURL %>%
read_html() %>% # parse the html
html_node(css='.wikitable.sortable') %>% # grab a class of object
html_table() %>% # convert table to data.frame
sapply(function(x) repair_encoding(as.character(x), 'UTF-8')) %>% # repair encoding; makes a matrix
data.frame() %>% # back to df
mutate(City = str_replace(City, '\\[(.*?)\\]', ''), # remove footnotes
latlon = sapply(str_split(Location, '/'), last), # split up location (3 parts)
latlon = str_extract_all(latlon, '[-|[0-9]]+\\.[0-9]+'), # grab any that start with - or number
lat = sapply(latlon, first), # grab latitudes
lon = sapply(latlon, nth, 2), # grab longitude
population2016 = as.numeric(str_replace_all(X2016.estimate, ',', '')), # remove commas from numbers (why do people do this?)
population2010 = as.numeric(str_replace_all(X2010.Census, ',', '')), # same for 2010
popDiff = round(population2016/population2010 - 1, 2)*100) %>% # create percentage difference
```
## Cont'd.
```{r wikileafletNoEval2, eval=FALSE}
select(-latlon, -Location) %>% # remove stuff we wouldn't ever use
filter(as.numeric(as.character(X2016.rank)) <= 50) %>% # top 50
plot_geo(locationmode = 'USA-states', sizes = c(1, 250)) %>% # map with plotly
add_markers(x = ~lon, y = ~lat, size = ~abs(popDiff), hoverinfo='text',
color=~popDiff, hoverinfo='text', colors='RdBu',
text=~hovertext, marker = list(opacity = 0.5)) %>%
layout(title = 'Largest US cities and their 2010-2016 change',
geo = g, paper_bgcolor='#fdf6e3')
# g is a list of map options, e.g. projection line colors etc.
```
## And the result...
```{r wikileaflet, eval=F, echo=FALSE}
library(rvest); library(stringr); library(leaflet)
'https://en.wikipedia.org/wiki/List_of_United_States_cities_by_population' %>%
read_html %>%
html_node(css='.wikitable.sortable') %>%
html_table %>%
sapply(function(x) repair_encoding(as.character(x), 'UTF-8'), simplify=F) %>%
data.frame %>%
mutate(City = str_replace(City, '\\[(.*?)\\]', ''),
latlon = sapply(str_split(Location, '/'), last),
latlon = str_extract_all(latlon, '[-|[0-9]]+\\.[0-9]+'),
lat = sapply(latlon, first),
lon = sapply(latlon, nth, 2),
population2016 = as.numeric(str_replace_all(X2016.estimate, ',', '')),
population2010 = as.numeric(str_replace_all(X2010.Census, ',', '')),
popDiff = round(population2016/population2010 - 1, 2)*100) %T>%
select(-latlon, -Location) %>%
filter(as.numeric(as.character(X2016.rank)) <= 50) %>%
leaflet %>%
addProviderTiles("CartoDB.DarkMatterNoLabels") %>%
setView(-94, 35, zoom = 4) %>%
addCircleMarkers(~lon, ~lat,
radius= ~scales::rescale(popDiff, c(2, 11)),
fillColor= ~colorNumeric(palette = c('Red', 'White', 'Navy'), popDiff)(popDiff),
stroke = FALSE, fillOpacity = .85,
popup= ~paste(City, paste0(popDiff, '%')))
```
```{r plotlyMap, echo=FALSE}
library(rvest); library(stringr); library(plotly)
g <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
showland = TRUE,
landcolor = toRGB("gray90"),
showlakes=F,
oceancolor= toRGB('red'),
subunitwidth = 1,
countrywidth = 1,
subunitcolor = toRGB('#fdf6e3'),
countrycolor = toRGB('#fdf6e3'),
bgcolor = toRGB('#fdf6e3')
)
'https://en.wikipedia.org/wiki/List_of_United_States_cities_by_population' %>%
read_html %>%
html_node(css='.wikitable.sortable') %>%
html_table %>%
sapply(function(x) repair_encoding(as.character(x), 'UTF-8'), simplify=F) %>%
data.frame %>%
mutate(City = str_replace(City, '\\[(.*?)\\]', ''),
latlon = sapply(str_split(Location, '/'), last),
latlon = str_extract_all(latlon, '[-|[0-9]]+\\.[0-9]+'),
lat = sapply(latlon, first),
lon = sapply(latlon, nth, 2),
population2016 = as.numeric(str_replace_all(X2016.estimate, ',', '')),
population2010 = as.numeric(str_replace_all(X2010.Census, ',', '')),
popDiff = round(population2016/population2010 - 1, 2)*100,
hovertext = paste(City, "<br />", paste0(popDiff, '%'))) %T>%
select(-latlon, -Location) %>%
filter(as.numeric(as.character(X2016.rank)) <= 50) %>%
plot_geo(locationmode = 'USA-states', sizes = c(1, 250)) %>%
add_markers(x = ~lon, y = ~lat, size = ~abs(popDiff), hoverinfo='text', color=~popDiff, hoverinfo='text', colors='RdBu',
text=~hovertext, marker = list(opacity = 0.5)) %>%
layout(title = 'Largest US cities and their 2010-2016 population change',
geo = g, paper_bgcolor='#fdf6e3')
```
##
For your own code, try a more concise approach
- Better to do the parsed page, data stuff, and plot as separate steps
However, it serves as an illustration of what's possible
>- Key commands: <span class="func">mutate</span>, <span class="func">select</span>, <span class="func">filter</span>
>- Autocomplete for unquoted variable names
>- Pipe to other functions (e.g. visualization)
## Newer approaches to data wrangling
Packages have been created to make data wrangling easier
We will focus on <span class='pack'>plyr</span>, <span class='pack'>dplyr</span>, and <span class='pack'>tidyr</span>
But others, e.g. <span class='pack'>data.table</span>, may be useful as well
Newer visualization packages work similarly
- Makes it easier to explore your data visually
## A provocation
<div style='font-size:150%'>
```{r provocation1, eval=F, echo=T}
c('Ceci', "n'est", 'pas', 'une', 'pipe!') %>%
{
.. <- . %>%
if (length(.) == 1) .
else paste(.[1], '%>%', ..(.[-1]))
..(.)
}
```
</div>
##
<div style='font-size:150%'>
```{r provocation2, eval=T, echo=T}
c('Ceci', "n'est", 'pas', 'une', 'pipe!') %>%
{
.. <- . %>%
if (length(.) == 1) .
else paste(.[1], '%>%', ..(.[-1]))
..(.)
}
```
</div>
## Your turn...
## Your turn
Let's get to it!
>- Use a base R dataset
- Examples: iris, mtcars, faithful or state.x77; <span style='font-family:monospace'>library(help='datasets')</span>
>- Pipe to something like the <span class='func'>summary</span>, <span class='func'>plot</span> or <span class='func'>cor</span> (if all numeric) as follows:
```{r yourturnPipe, eval=FALSE}
data %>%
function
```
>- Use additional arguments if you want:
```{r pipewithargs, eval=FALSE}
data %>%
function(arg='blah')
```
Note that Ctrl+Shft+m is the shortcut to make the %>% pipe.
## Example
```{r yourturnPipeExample, eval=TRUE, cache=TRUE}
iris %>% summary
```