-
Notifications
You must be signed in to change notification settings - Fork 1
/
Day2_Summaries.Rmd
300 lines (223 loc) · 8.32 KB
/
Day2_Summaries.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
## The `select` function
```{r Day2-Summaries-1, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, comment = "")
library(rio)
library(tidyverse)
```
The `select` function selects variables (columns) from your dataset. We will look
at some nice selection methods using a dataset of all foreign aid disbursements from
Department of State between 2004 and 2017.
```{r Day2-Summaries-2 }
library(rio)
dos <- import('data/Department of State.csv') %>% as_tibble()
names(dos)
```
There are several groups of variables by name, and `dplyr` provides nice functions
to extract them.
```{r Day2-Summaries-3, echo=F}
dos %>% select(starts_with("Award"))
```
```{r Day2-Summaries-4, echo=T}
dos %>% select(ends_with("Value"))
```
```{r Day2-Summaries-5, echo=T}
dos %>% select(contains("Transaction"))
```
There are several other helper functions that can grab variables based on
properities of their names.
- starts_with(): Starts with a prefix.
- ends_with(): Ends with a suffix.
- contains(): Contains a literal string.
- matches(): Matches a regular expression.
- num_range(): Matches a numerical range like x01, x02, x03.
- one_of(): Matches variable names in a character vector.
- everything(): Matches all variables.
- last_col(): Select last variable, possibly with an offset.
We can pipe these too. In the next bit, we are trying to get the variables that
representing start dates. We are using the `pull` function to grab the data out of the dataset
in the form of an array, just so it's easier to manipulate.
```{r Day2-Summaries-6 }
start_dates <- dos %>%
select(ends_with("Date")) %>%
select(contains("Start")) %>%
pull(1)
head(start_dates)
```
## Dates
__Resource__: [RStudio cheatsheet](https://rawgit.com/rstudio/cheatsheets/master/lubridate.pdf)
Dates are a first-class data type in R, though they are natively somewhat difficult to
manage. The package `lubridate` added several convenience functions to make this work
much easier.
```{r Day2-Summaries-7 }
library(lubridate)
start_dates <- as_date(start_dates) %>% head()
start_dates
year(start_dates)
month(start_dates)
day(start_dates)
```
```{r Day2-Summaries-8 }
sort(start_dates)
quarter(start_dates)
days(start_dates) - days(as_date('2011-10-01')) # Days from start of fiscal year
```
So we see that we can do math with dates to identify intervals of time, in
appropriate units.
# Joins
We are often in the situation that pieces of data are resident in different tables,
and we need to put them together for analyses. Usually each table will have some type
of identifier variable that identifies the unit with which each observation is
associated. Joins use these identifiers to put datasets together while maintaining
correspondence of the data with the actual units.
There are 4 primary kinds of joins:
![](img/joins.png)
Let's summarize what can happen with joins:
```{r Day2-Summaries-9, echo=F}
d <- tribble(
~ Type, ~Rows, ~Columns,
'inner', 'same or decrease', 'increase',
'left', 'same as left dataset','increase',
'right', 'same as right dataset','increase',
'full','increase','increase')
knitr::kable(d)
```
We'll play with two simulated datasets looking at staffing and real estate allocation at
DOS. The question we're asking is, what is the area avaiable per capita in each Bureau.
```{r Day2-Summaries-10 }
staffing_data <- import('data/Staffing_by_Bureau.csv')
real_estate <- import('data/DoS_Real_Estate_Allocation.csv')
```
```{r Day2-Summaries-11 }
staffing_data %>% as_tibble()
real_estate %>% as_tibble()
```
First we summarize the data by Bureau
```{r Day2-Summaries-12, message=T}
staff_summary <- staffing_data %>%
group_by(Bureau) %>%
tally(name = 'Pop')
realestate_summary <- real_estate %>%
group_by(Bureau) %>% summarize(Size = sum(Size))
```
```{r Day2-Summaries-13 }
staff_summary %>% head(4)
realestate_summary %>% head(4)
```
Then we join the two datasets together. They will join based on the Bureau variable,
which is common to both datasets.
```{r Day2-Summaries-14 }
staff_summary %>%
inner_join(realestate_summary, by = c("Bureau" = "Bureau"))
```
Once we have joined the datasets, we can now create a variable that computes the area per capita, and then we sort the rows of the data in order of descending area per capita.
```{r Day2-Summaries-15 }
staff_summary %>%
inner_join(realestate_summary, by = c("Bureau" = "Bureau")) %>%
mutate(unit_area = Size/Pop) %>%
arrange(unit_area)
```
# Data summaries
The `dplyr` package gives us 5 verbs to operate on a single data frame:
- `filter`: filter a dataset by rows
- `select`: select columns of a dataset
- `arrange`: arrange rows of a dataset by values of some variables
- `group_by`: split a dataset by values of some variables, so that we can apply verbs to each split
- `summarize`: compute various summaries from the data
It also gives us verbs for joining 2 data frames:
- `left_join`
- `right_join`
- `inner_join`
- `outer_join`
- `semi_join` : Keep rows of left dataset which correspond to rows in right dataset
- `anti_join` : Keep rows of left dataset which __do not__ correspond to rows in right dataset
- `bind_rows` : Stack two datasets on top of each other, provided they have the same number of columns
- `bind_cols` : Put two datasets side by side, provided they have the same number or rows.
]
```{r Day2-Summaries-16 }
library(tidyverse)
mtcars1 <- mtcars %>% rownames_to_column('cars') %>% as_tibble()
mtcars1
```
We can compute different summaries over the variables in a dataset.
```{r Day2-Summaries-17 }
mtcars1 %>% summarize(mpg = mean(mpg, na.rm=T), disp = mean(disp, na.rm=T), hp = mean(hp, na.rm=T))
```
## Scoped verbs
All the `dplyr` verbs have scoped versions `*_all`, `*_at` and `*_if`.
1. `*_all` : Act on all columns
1. `*_at` : Act on specified columns
1. `*_if` : Act on columns with specific property
## Factors (categorical variables)
`factor` types of variables are discrete or categorical variables, that only take
a small set of values. Think number of cylinders in a car, race, sex.
We can covert a variable to a factor using `as.factor`. (There are also
`as.numeric` and `as.character`).
```{r Day2-Summaries-18 }
dos %>% mutate_at(vars(ends_with("Date")), as_date) %>%
summarise_if(is.Date, max)
```
Here, we notice that a couple of the values are `NA`. We would like to add the
`na.rm=T` option into the mean function. We can do that, as long as we put a `~` before
it. The `.` is a place holder for each variable that will be interrogated here.
```{r Day2-Summaries-19 }
dos %>% mutate_at(vars(ends_with("Date")), as_date) %>%
summarize_at(vars(ends_with("Date")), ~max(., na.rm=T))
```
```{r Day2-Summaries-20 }
mtcars1 <- mtcars1 %>%
mutate_at(vars(cyl, vs, am, gear, carb), as.factor)
str(mtcars1)
```
```{r Day2-Summaries-21 }
mtcars1 %>% summarize_if(is.numeric, mean)
```
# Split-apply-combine
![](img/split-apply-combine.png)
Here, we compute summaries (means) by levels of cylinders.
```{r Day2-Summaries-22 }
mtcars1 %>%
group_by(cyl) %>%
summarize(mpg_mean = mean(mpg))
```
We can still use scoped verbs if we want.
```{r Day2-Summaries-23 }
mtcars1 %>%
group_by(cyl) %>%
summarize_if(is.numeric, mean)
```
We can also do several summaries in one go.
```{r Day2-Summaries-24 }
mtcars1 %>%
group_by(cyl) %>%
summarize_if(is.numeric, list('mean' = mean, 'median' = median))
```
Let's go back to the DOS dataset. We're going to see how much money was given to
each implementatoin organization over the years.
```{r Day2-Summaries-25 }
dos %>%
group_by(Implementing_Organization) %>%
summarize(amt = sum(Award_Transaction_Value)) %>%
arrange(desc(amt))
```
We can also split by organization type
```{r Day2-Summaries-26 }
dos %>%
group_by(Implementing_Organization_Type) %>%
summarize(amt = sum(Award_Transaction_Value)) %>%
arrange(desc(amt))
```
If we want to look at time trends, we will also need to split by year, in addition to
the organization. We're using some `lubridate` code to make the years accessible to
the computer.
```{r Day2-Summaries-27 }
dos_by_year <- dos %>%
group_by(Implementing_Organization, year = year(as_date(Award_Start_Date))) %>%
summarize(amt = sum(Award_Transaction_Value)) %>%
filter(Implementing_Organization != '', !is.na(year))
```
We can also look at the overall trends by year.
```{r Day2-Summaries-28 }
dos_by_year %>%
group_by(year) %>%
summarize(amt = sum(amt))
```