-
Notifications
You must be signed in to change notification settings - Fork 2
/
notes_01.R
220 lines (151 loc) · 4.67 KB
/
notes_01.R
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
# Notes/live coding from Day 1
# to be filled in as we go
library(tidyverse)
# 02_wrangling section ----
wq <- read.csv(here::here("data", "daily_wq.csv"), stringsAsFactors = FALSE)
ebird <- read.csv(here::here("data", "eBird_workshop.csv"), stringsAsFactors = FALSE)
ebird <- dplyr::distinct(ebird)
#filter ----
ebird2008 <- ebird %>%
filter(year == 2008)
ebirdAK <- ebird %>%
filter(state == "AK")
ebirdAK2008 <- ebird %>%
filter(state == "AK",
year == 2008)
ebirdMS2018 <- ebird %>%
filter(year == 2018,
state == "MS")
ebirdMSandAK <- ebird %>%
filter(state %in% c("AK", "MS"))
my_states <- c("AK", "MS")
ebird_again <- ebird %>%
filter(state %in% my_states)
ebird %>%
filter(year < 2010,
state == "AK")
your_turn_1 <- ebird %>%
filter(species == "American Coot",
year != 2010,
state %in% c("MS", "FL"))
# DON'T do this:
your_turn_1b <- ebird %>%
filter(species == "American Coot",
year != 2010,
state == c("MS", "FL"))
# select ----
ebird_select1 <- ebird %>%
select(species, state)
ebird_select2 <- ebird %>%
select(state, species)
ebird_select3 <- ebird %>%
select(-samplesize, -presence)
head(ebird_select3)
ebird_select4 <- ebird %>%
select(state, year, species, samplesize, presence)
head(ebird_select4)
ebird_select5 <- ebird %>%
select(year, everything())
head(ebird_select5)
your_turn_2 <- ebird %>%
filter(state %in% c("FL", "AL", "MS"),
species == "American Coot") %>%
select(state, year, presence)
head(your_turn_2)
# this will cause an error
ebird %>%
select(state, year, presence) %>%
filter(state %in% c("FL", "AL", "MS"),
species == "American Coot")
# your turn 3 ----
wq_trimmed <- wq %>%
select(station_code, month, day, temp, sal, do_pct, depth) %>%
filter(!is.na(depth))
summary(wq$depth)
mean(wq$depth)
mean(wq$depth, na.rm = TRUE)
test <- c(1, 2, 3, 4, 5)
test2 <- c(1, 2, NA, 4, 5)
is.na(test)
is.na(test2)
is.na(wq$depth)
!is.na(wq$depth)
# create new column named depth_ft
wq_trimmed2 <- wq_trimmed %>%
mutate(depth_ft = depth * 3.28)
head(wq_trimmed2)
# modify existing depth column
wq_trimmed3 <- wq_trimmed %>%
mutate(depth = depth * 3.28)
head(wq_trimmed3)
wq_trimmed <- wq_trimmed %>%
mutate(depth_ft = depth * 3.28)
wq_trimmed <- wq_trimmed %>%
mutate(monthday = paste(month, day, sep = "-"),
meaningless = sal + temp,
even_more_meaningless = meaningless + 5)
# to remove objects from your environment,
# use rm(object_to_remove)
# to remove columns from a data frame,
# use select(-column_to_remove)
wq_trimmed <- wq_trimmed %>%
select(-monthday,
-meaningless,
-even_more_meaningless) %>%
mutate(temp_f = (9/5)*temp + 32)
# group by and summarize ----
# one level: state
# should result in 50 rows
# (one for each state)
ebird_grouped <- ebird %>%
group_by(state) %>%
summarize(mean_presence = mean(presence, na.rm = TRUE),
max_presence = max(presence, na.rm = TRUE)
)
# can group on multiple levels
ebird_grouped <- ebird %>%
group_by(state, species) %>%
summarize(mean_presence = mean(presence, na.rm = TRUE),
max_presence = max(presence, na.rm = TRUE)
)
wq_trimmed %>%
group_by(station_code) %>%
summarize(mean_depth = mean(depth, na.rm = TRUE))
test <- c(1, 2, 3, 4, 5)
mean(test)
sd(test)
wq_summary <- wq_trimmed %>%
group_by(station_code, month) %>%
summarize(mean_temp = mean(temp, na.rm = TRUE),
sd_temp = sd(temp, na.rm = TRUE),
mean_sal = mean(sal, na.rm = TRUE),
sd_sal = sd(sal, na.rm = TRUE))
# WHEN YOU GET BACK FROM BREAK
# please copy the code below from the notes page
# or just type it into your script window
# pivoting ----
fish <- read.csv(here::here("data", "guana_fish.csv"),
stringsAsFactors = FALSE)
# subset it
fish_sub <- fish %>%
select(Date,
Site,
Diel,
starts_with("Cteno"),
contains("penaeus"))
fish_long <- fish_sub %>%
pivot_longer(4:8, names_to = "species",
values_to = "count")
ggplot(fish_long) +
geom_col(mapping = aes(x = species, y = count, fill = species)) +
facet_wrap(~ Site) +
theme(axis.text = element_text(angle = 90))
# skipping a step.
fish_sub %>%
pivot_longer(4:8, names_to = "species",
values_to = "count") %>%
ggplot() +
geom_col(mapping = aes(x = species, y = count, fill = species)) +
facet_wrap(~ Site) +
theme(axis.text = element_text(angle = 90))
bit.ly/finalchallenge_day1