forked from ucsb-bren/ESM296-3W-2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwk04_tidyr.Rmd
329 lines (228 loc) · 9.49 KB
/
wk04_tidyr.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
---
title: 'Week 4: Tidying up Data'
author: "Ben Best"
date: '`r format(Sys.time(), "%Y-%m-%d %H:%M")`'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval=FALSE)
```
## Precursors
### Final Project
The instructions for your class group's [Final Project](./#final-project) have been updated.
### Schedule
The class for week 9 on March 4th conflicts with the [Bren Group Project](http://www.bren.ucsb.edu/research/gp_submit.html) presentations, so we'll be extending the classes before and after by 1 hour.
### Invitations
- **organizations**: invite @bbest and @naomitague to your `github.com/<org>` so we can directly _push_ to your site (vs _fork_ & _pull request_)
- **auditors**: email [email protected] to ensure you get announcements via GauchoSpace
### `setwd()`
As mentioned in [wk03_dplyr](./wk03_dplyr.html#where-am-i?-getting-around-in-the-command-line), the working directory when knitting your Rmarkdown file is always the folder in which it is contained, eg for `env-info/students/bbest.Rmd` the working directory is `env-info/students`. This may be different from your R Console in RStudio which defaults to the working directory to the top level folder of your project, ie `env-info`. To get the two to be the same as you test code in the Console before knitting the Rmarkdown to HTML, I **REQUIRE** you to insert the following R chunk near the beginning (but after the Rmarkdown front matter surrounded by the rows `---`) of your `env-info/students/<user>.Rmd`:
```{r setwd students}
# set working directory if has students directory and at R Console (vs knitting)
if ('students' %in% list.files() & interactive()){
setwd('students' )
}
# ensure working directory is students
if (basename(getwd()) != 'students'){
stop(sprintf("WHOAH! Your working directory is not in 'students'!\n getwd(): %s", getwd()))
}
```
This then ensures that "relative" paths will work the same in your R Console as when knitting your Rmarkdown to HTML. For instance:
```{r}
# absolute: /Users/bbest/github/env-info/students/data/bbest_ports-bc.csv
d = read.csv('./data/bbest_ports-bc.csv') # ./data is child of students
# absolute: /Users/bbest/github/env-info/data/r-ecology/surveys.csv
d = read.csv('../data/r-ecology/surveys.csv') # ../data is sibling of students
```
The first path uses this folder `.` since that `data` folder is a "child" of the `students` folder, whereas the second path backs up a folder `..` before descending into the other `data` folder that is a "sibling" of the `students` folder.
### Assignment (Individual)
For the data wrangling portion of today, append the header `## 4. Tidying up Data` to your `env-info/students/<user>.Rmd` and include R chunks to run the demo below and give yourself the opportunity to try out possibilities with the code. Please set aside another header below this section `## 4. Answers and Tasks` where you answer questions and perform tasks on applying the functions to the CO<sub>2</sub> dataset as R chunks. Please include the question or task above the R chunk or answer.
You'll find it easiest to copy and paste the demo portion from [`env-info/wk04_tidyr.Rmd`](https://raw.githubusercontent.com/ucsb-bren/env-info/gh-pages/wk04_tidyr.Rmd) but will need to understand this material enough to apply to the questions and tasks.
You will want to synchronize with `ucsb-bren/env-info` (ie _pull request_ `ucsb-bren/env-info` to `<user>/env-info`, _merge_ the pull request in `<user>/env-info`, and _pull_ to update your local machine), in order to successfully knit your `env-info/students/<user>.Rmd`. The Rmarkdown below expects `env-info/wk04_tidyr/img/data-wrangling-cheatsheet_tidyr.png` and `env-info/data/co2_europa.xls` which are in the updated `ucsb-bren/env-info`.
## data
The R chunks explaining the `dplyr` and `tidyr` functions below are pulled from the excellent [**wrangling-webinar.pdf**](wk03_dplyr/wrangling-webinar.pdf) presentation, which you should consult as you execute (see shortcuts in [rstudio-IDE-cheatsheet.pdf](refs/cheatsheets/rstudio-IDE-cheatsheet.pdf)).
### EDAWR
```{r EDAWR}
# install.packages("devtools")
# devtools::install_github("rstudio/EDAWR")
library(EDAWR)
help(package='EDAWR')
?storms # wind speed data for 6 hurricanes
?cases # subset of WHO tuberculosis
?pollution # pollution data from WHO Ambient Air Pollution, 2014
?tb # tuberculosis data
View(storms)
View(cases)
View(pollution)
```
### slicing
```{r traditional R slicing}
# storms
storms$storm
storms$wind
storms$pressure
storms$date
# cases
cases$country
names(cases)[-1]
unlist(cases[1:3, 2:4])
# pollution
pollution$city[c(1,3,5)]
pollution$amount[c(1,3,5)]
pollution$amount[c(2,4,6)]
# ratio
storms$pressure / storms$wind
```
```{r dplyr on storms}
# better yet
library(dplyr)
pollution %>%
filter(city != 'New York') %>%
mutate(
ratio = pressure / wind)
```
## tidyr
Two main functions: gather() and spread()
```{r tidyr}
# install.packages("tidyr")
library(tidyr)
?gather # gather to long
?spread # spread to wide
```
### `gather`
```{r gather}
cases
gather(cases, "year", "n", 2:4)
gather(cases, "year", "n", -1)
```
### `spread`
```{r spread}
pollution
spread(pollution, size, amount)
```
Other functions to extract and combine columns...
### `separate`
```{r separate}
storms
storms2 = separate(storms, date, c("year", "month", "day"), sep = "-")
```
### `unite`
```{r unite}
storms2
unite(storms2, "date", year, month, day, sep = "-")
```
**Recap: tidyr**:
- A package that reshapes the layout of data sets.
- Make observations from variables with `gather()` Make variables from observations with `spread()`
- Split and merge columns with `unite()` and `separate()`
From the [data-wrangling-cheatsheet.pdf](./refs/cheatsheets/data-wrangling-cheatsheet.pdf):
![](wk04_tidyr/img/data-wrangling-cheatsheet_tidyr.png)
### tidy CO<sub>2</sub> emissions
_**Task**. Convert the following table [CO<sub>2</sub> emissions per country since 1970](http://edgar.jrc.ec.europa.eu/overview.php?v=CO2ts1990-2014&sort=des9) from wide to long format and output the first few rows into your Rmarkdown. I recommend consulting `?gather` and you should have 3 columns in your output._
```{r read co2}
library(dplyr)
library(readxl) # install.packages('readxl')
# xls downloaded from http://edgar.jrc.ec.europa.eu/news_docs/CO2_1970-2014_dataset_of_CO2_report_2015.xls
xls = '../data/co2_europa.xls'
print(getwd())
co2 = read_excel(xls, skip=12)
co2
```
_**Question**. Why use `skip=12` argument in `read_excel()`?_
## dplyr
A package that helps transform tabular data
```{r dplyr}
# install.packages("dplyr")
library(dplyr)
?select
?filter
?arrange
?mutate
?group_by
?summarise
```
See sections in the [data-wrangling-cheatsheet.pdf](./refs/cheatsheets/data-wrangling-cheatsheet.pdf):
- Subset Variables (Columns), eg `select()`
- Subset Observations (Rows), eg `filter()`
- Reshaping Data - Change the layout of a data set, eg `arrange()`
- Make New Variables, eg `mutate()`
- Group Data, eg `group_by()` and `summarise()`
### `select`
```{r select}
storms
select(storms, storm, pressure)
storms %>% select(storm, pressure)
```
### `filter`
```{r filter}
storms
filter(storms, wind >= 50)
storms %>% filter(wind >= 50)
storms %>%
filter(wind >= 50) %>%
select(storm, pressure)
```
### `mutate`
```{r mutate}
storms %>%
mutate(ratio = pressure / wind) %>%
select(storm, ratio)
```
### `group_by`
```{r group_by}
pollution
pollution %>% group_by(city)
```
### `summarise`
```{r summarise}
# by city
pollution %>%
group_by(city) %>%
summarise(
mean = mean(amount),
sum = sum(amount),
n = n())
# by size
pollution %>%
group_by(size) %>%
summarise(
mean = mean(amount),
sum = sum(amount),
n = n())
```
note that `summarize` synonymously works
### `ungroup`
```{r ungroup}
pollution %>%
group_by(size)
pollution %>%
group_by(size) %>%
ungroup()
```
### multiple groups
```{r multiple groups}
tb %>%
group_by(country, year) %>%
summarise(cases = sum(cases))
summarise(cases = sum(cases))
```
**Recap: dplyr**:
- Extract columns with `select()` and rows with `filter()`
- Sort rows by column with `arrange()`
- Make new columns with `mutate()`
- Group rows by column with `group_by()` and `summarise()`
See sections in the [data-wrangling-cheatsheet.pdf](./refs/cheatsheets/data-wrangling-cheatsheet.pdf):
- Subset Variables (Columns), eg `select()`
- Subset Observations (Rows), eg `filter()`
- Reshaping Data - Change the layout of a data set, eg `arrange()`
- Make New Variables, eg `mutate()`
- Group Data, eg `group_by()` and `summarise()`
### summarize CO<sub>2</sub> emissions
_**Task**. Report the top 5 emitting countries (not World or EU28) for **2014** using your long format table. (You may need to convert your year column from factor to numeric, eg `mutate(year = as.numeric(as.character(year)))`. As with most analyses, there are multiple ways to do this. I used the following additional functions: `filter`, `arrange`, `desc`, `head`)_.
_**Task**. Summarize the **total** emissions by country (not World or EU28) across years from your long format table and return the top 5 emitting countries. (As with most analyses, there are multiple ways to do this. I used the following functions: `filter`, `group_by`, `summarize`, `sum`, `arrange`, `desc`, `head`)_.
## joining data
Next week, we'll do a bit on joining data.
## References
### Data Wrangling in R
- [Data Wrangling (dplyr, tidyr) cheat sheet]({{ site.baseurl }}/refs/cheatsheets/data-wrangling-cheatsheet.pdf)
- [wrangling-webinar.pdf](wrangling-webinar.pdf)