-
Notifications
You must be signed in to change notification settings - Fork 3
/
02-notes.Rmd
247 lines (171 loc) · 4.92 KB
/
02-notes.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
---
layout: topic
title: Aggregating and analyzing data with dplyr (notes)
author: Data Carpentry contributors
---
```{r, echo=FALSE}
knitr::opts_chunk$set(results='hide', fig.path='img/r-lesson-')
surveys <- read.csv("data/portal_data_joined.csv")
```
## Key idea
All that bracket-based selecting can be a bit cumbersome.
Add-on package dplyr greatly simplifies the process; inspired by SQL.
`select`, `filter`, `mutate`, `group_by`, `summarize`
(also `tally` and `arrange`)
## Install and load the library
```{r eval=FALSE}
install.packages("dplyr")
install.packages("ggplot2")
```
```{r, message=FALSE}
library(dplyr)
```
## Select and filter
Select to grab columns.
```r
selectedcol <- select(surveys, species_id, plot_type, weight)
head(selectedcol)
```
Filter to grab rows.
```{r filter}
surveys2002 <- filter(surveys, year==2002)
head(surveys2002)
```
## Pipe
Output of one function becomes the input to the next.
```{r}
surveys %>%
filter(weight < 5) %>%
select(species_id, sex, weight)
```
<kbd>`Ctrl`</kbd> + <kbd>`Shift`</kbd> + <kbd>`M`</kbd> to insert `%>%`
Could assign this to something:
```{r}
surveys_sml <- surveys %>%
filter(weight < 5) %>%
select(species_id, sex, weight)
```
### Challenge
Using pipes, subset the data to include individuals collected before 1995,
and retain the columns `year`, `sex`, and `weight.`
## Mutate
`mutate()` to derive a new column.
```{r}
surveys %>%
mutate(weight_kg = weight / 1000)
```
To just look at the top:
```{r}
surveys %>%
mutate(weight_kg = weight / 1000) %>%
head
```
Filter out `NA`s:
```{r}
surveys %>%
filter(!is.na(weight)) %>%
mutate(weight_kg = weight / 1000) %>%
head
```
### Challenge
Create a new dataframe from the survey data that meets the following
criteria: contains only the `species_id` column and a column that contains
values that are the square-root of `hindfoot_length` values (e.g. a new column
`hindfoot_sqrt`). In this `hindfoot_sqrt` column, there are no NA values
and all values are < 3.
Hint: think about how the commands should be ordered
### split-apply-combine data analyses (group-by and summarize)
Many analyses fit a split-apply-combine pattern: split the data into
groups, apply some analysis to each group, and then combine the
results.
With dplyr, we use `group_by()` for the splitting and `tally` or
`summarize()` for the rest.
Count individuals by sex:
```{r}
surveys %>%
group_by(sex) %>%
tally()
```
Average weight by sex:
```{r}
surveys %>%
group_by(sex) %>%
summarize(mean_weight = mean(weight, na.rm = TRUE))
```
Can group by multiple columns:
```{r}
surveys %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight, na.rm = TRUE))
```
Maybe filter those `NA`s
```{r}
surveys %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight, na.rm = TRUE)) %>%
filter(!is.na(mean_weight))
```
Another thing we might do here is sort rows by `mean_weight`, using
`arrange()`.
```{r}
surveys %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight, na.rm = TRUE)) %>%
filter(!is.na(mean_weight)) %>%
arrange(mean_weight)
```
If you want them sorted from highest to lowest, use `desc()`.
```{r}
surveys %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight, na.rm = TRUE)) %>%
filter(!is.na(mean_weight)) %>%
arrange(desc(mean_weight))
```
Also note that you can include multiple summaries.
```{r}
surveys %>%
group_by(sex, species_id) %>%
summarize(mean_weight = mean(weight, na.rm = TRUE),
min_weight = min(weight, na.rm = TRUE)) %>%
filter(!is.na(mean_weight)) %>%
arrange(desc(mean_weight))
```
### Challenge
How many times was each `plot_type` surveyed?
### Challenge
Use `group_by()` and `summarize()` to find the mean, min, and max hindfoot
length for each species.
### Challenge
What was the heaviest animal measured in each year? Return the columns `year`,
`genus`, `species`, and `weight`.
## Data cleaning preparations
In preparations for the plotting, let's do a bit of data cleaning:
remove rows with missing `species_id`, `weight`, `hindfoot_length`, or
`sex`.
```{r clean_data_1}
surveys_complete <- surveys %>%
filter(species_id != "", !is.na(weight)) %>%
filter(!is.na(hindfoot_length), sex != "")
```
There are a lot of species with low counts. Let's remove the species
with less than 10 counts.
```{r}
# count records per species
species_counts <- surveys_complete %>%
group_by(species_id) %>%
tally
head(species_counts)
# get names of the species with counts >= 10
frequent_species <- species_counts %>%
filter(n >= 10) %>%
select(species_id)
# filter out the less-frequent species
surveys_complete <- surveys_complete %>%
filter(species_id %in% frequent_species$species_id)
```
We might save this to a file:
```{r save_reduced_data_to_file, eval=FALSE}
write.csv(reduced, "CleanData/portal_data_reduced.csv")
```
<br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/>