-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay1-DataWrangling-COMPLETE.R
More file actions
298 lines (196 loc) · 7.05 KB
/
Day1-DataWrangling-COMPLETE.R
File metadata and controls
298 lines (196 loc) · 7.05 KB
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
### Limpopo Resilience Lab Virtual R Workshop
### August 18-20, 2021
### Instructor: Max Glines
### orginal code created by Rachel Pilla
### DAY 2: Data Wrangling ###
library(dplyr)
library(tidyr)
#OR
library(tidyverse)
setwd("")
beit.bridge <- read.csv("A7H008YRPK.CSV", skip = 7)
beit.bridge.clean <- beit.bridge[1:28, ]
#########################
### Manipulating data ###
#########################
# dplyr: four main functions and pipe tool
# data frame name is ALWAYS the first argument for these if using alone
?select
?filter
?mutate
?summarize
## choose specific column(s) by name
beit.bridge.level <- select(beit.bridge.clean, Year, Level..m.)
beit.bridge.level
## choose row(s) by condition
Year2000 <- filter(beit.bridge.clean, Year == 2000)
Year2000
#operators for filter
# greater than >
# greater than or equal to >=
# less than <
# less than or equal to <=
# equal ==
# not equal to !=
# and &
# or |
# to filter NA values, use is.na()
Year2010_2015 <- filter(beit.bridge.clean, Year >= 2010 & Year <= 2015)
Year2010_2015
YearExtremes <- filter(beit.bridge.clean, Level..m. < 1 | Level..m. > 3)
YearExtremes
beit.bridge.filterNA <- filter(beit.bridge, !is.na(Date))
# create new column(s)
beit.bridge.LperS <- mutate(beit.bridge.clean, Flow_LperS = Flow..cumec.*1000)
beit.bridge.LperS
# summarize (must become fewer rows than original)
MeanHydrol <- summarize(beit.bridge.LperS,
MeanLevel_m = mean(Level..m.),
MeanFlow_LperS = mean(Flow_LperS))
MeanHydrol
# pipe tool for multiple steps at once
# do NOT need to name data frame if using this, since it will automatically start with the
# data frame remaining from the previous line of code
# Let's say we wanted to find the maximum Level from the past 10 years only
RecentMaxLevel <- beit.bridge.clean %>%
filter(Year > 2010) %>%
select(Year, Level..m.) %>%
summarize(MaxLevel = max(Level..m.))
RecentMaxLevel
# PRACTICE
# filter the Years when the Level was greater than 2 m
# AND the flow was greater than 1000 m3/s
HighLevelFlowYears <- beit.bridge.clean %>%
filter(Level..m. > 2,
Flow..cumec. > 1000) %>%
select(Year)
# PRACTICE
# using the dplyr tools, create a new object
# that gives the MIN flow in L/s before 2000
MinFlow_pre2000 <- beit.bridge.clean %>%
filter(Year < 2000) %>%
mutate(Flow_LperS = Flow..cumec.*1000) %>%
summarize(MinFlow = min(Flow_LperS),
MaxFlow = max(Flow_LperS),
MeanFlow = mean(Flow_LperS))
MinFlow_pre2000
# DATA FORMATTING -- wide vs. long
# reformatting from wide to long using "pivot_longer"
beit.bridge.long <- beit.bridge.clean %>%
mutate(Flow_LperS = Flow..cumec.*1000) %>%
select(Year, Level..m., Flow..cumec., Flow_LperS) %>%
pivot_longer(cols = c(Level..m., Flow..cumec., Flow_LperS),
names_to = "Variable",
values_to = "Value")
beit.bridge.long
beit.bridge.wide <- beit.bridge.long %>%
pivot_wider(names_from = Variable,
values_from = Value)
beit.bridge.wide
##########################
### DATES & TIMES IN R ###
##########################
str(beit.bridge.clean)
library(lubridate)
?lubridate
?ymd
ymd(beit.bridge.clean$Date)
hm(beit.bridge.clean$Time)
paste(beit.bridge.clean$Date,beit.bridge.clean$Time)
beit.bridge.datetime <- beit.bridge.clean %>%
mutate(Date = ymd(beit.bridge.clean$Date),
Date_Time = ymd_hm(paste(Date,Time)))
## Work with another new data set: "Limpopo_Resilience_Lab__Mutale_Weir_Dataset.csv"
mutale <- read.csv("Limpopo_Resilience_Lab__Mutale_Weir_Dataset.csv")
head(mutale)
str(mutale)
# cleaning steps:
# 1) create DateTime column using lubridate
# 2) convert "-9999" and "-8888" to NA
# 3) remove columns Year through Minutes
# 4) rename columns to be clear and include units
mutale.clean <- mutale %>%
mutate(Date_Time = ymd_hm(paste(YEAR, MONT, DAYN, HOUR, MINU))) %>%
na_if(-9999) %>%
na_if(-8888) %>%
na_if(-7777) %>%
select(-YEAR, -MONT, -DAYN, -HOUR, -MINU) %>%
rename(Precipitation_mm = PRCP,
AirTemp_degC = TEMP,
RelHumidity_percent = RHMD,
SolarRad_W_m2 = SRAD,
AirPressure_kPa = APRS,
WindSpeed_m_s = WSPD,
WindDir_deg = WDIR,
RiverStage_m = RIVS,
WaterTemp_degC = WTMP,
Conductivity_uS_cm = COND,
Turbidity_NTU = TRBD)
str(mutale.clean)
# another example of converting wide to LONG format:
mutale.long <- mutale.clean %>%
pivot_longer(cols=Precipitation_mm:Turbidity_NTU,
names_to = "Variable",
values_to = "Value")
str(mutale.long)
# GROUPING data before summarizing
mutale.summary <- mutale.long %>%
group_by(Variable) %>%
summarize(MeanValue = mean(Value, na.rm = TRUE))
mutale.summary
##########################
### PRACTICE EXERCISES ###
##########################
## EXPLORE THE MUTALE WEIR DATA WE JUST CLEANED
## using DPLYR functions
library(tidyr)
library(dplyr)
setwd("")
mutale <- read.csv("Limpopo_Resilience_Lab__Mutale_Weir_Dataset.csv")
mutale.clean <- mutale %>%
mutate(Date_Time = ymd_hm(paste(YEAR, MONT, DAYN, HOUR, MINU))) %>%
na_if(-9999) %>%
na_if(-8888) %>%
na_if(-7777) %>%
select(-YEAR, -MONT, -DAYN, -HOUR, -MINU) %>%
rename(Precipitation_mm = PRCP,
AirTemp_degC = TEMP,
RelHumidity_percent = RHMD,
SolarRad_W_m2 = SRAD,
AirPressure_kPa = APRS,
WindSpeed_m_s = WSPD,
WindDir_deg = WDIR,
RiverStage_m = RIVS,
WaterTemp_degC = WTMP,
Conductivity_uS_cm = COND,
Turbidity_NTU = TRBD)
mutale.long <- mutale.clean %>%
pivot_longer(cols=Precipitation_mm:Turbidity_NTU,
names_to = "Variable",
values_to = "Value")
## 1) What was the range (minimum, maximum, and max-min difference) of
## each weather and hydrology variable measured?
mutale.range <- mutale.long %>%
group_by(Variable) %>%
summarize(MinValue = min(Value, na.rm = T),
MaxValue = max(Value, na.rm = T),
Range = MaxValue - MinValue)
mutale.range <- mutale.long %>%
group_by(Variable) %>%
summarize(MinValue = min(Value, na.rm = T),
MaxValue = max(Value, na.rm = T)) %>%
mutate(Range = MaxValue - MinValue)
mutale.range
## 2) Find the date when the river stage was the highest and the lowest.
HighLowRiverStage <- mutale.clean %>%
filter(RiverStage_m == max(RiverStage_m, na.rm = T) |
RiverStage_m == min(RiverStage_m, na.rm = T)) %>%
select(Date_Time, RiverStage_m)
HighLowRiverStage
## 3) Calculate the total cumulative precipitation for each month of the data set.
MonthlyPrecip <- mutale.clean %>%
mutate(Year = year(Date_Time),
Month = month(Date_Time)) %>%
group_by(Year, Month) %>%
summarize(TotalPrecip = sum(Precipitation_mm, na.rm = T))
MonthlyPrecip