-
Notifications
You must be signed in to change notification settings - Fork 0
/
HABSpring2024.Rmd
5199 lines (3524 loc) · 167 KB
/
HABSpring2024.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "HAB_Spring2024"
output: html_document
date: "2024-04-03"
editor_options:
chunk_output_type: console
---
#Library
```{r}
library(tidyverse)
library(googlesheets4)
library(oce)
library(padr)
library(rEDM)
library(ggplot2)
library(zoo)
library(quantreg)
```
```{r}
# Assuming 'ag_df_CR' is your data frame
# Create an empty list to store the results
lm_list <- list()
# List of variables to include in the linear models
vars <- c("ph", "Celsius", "spcond", "do", "turb", "Salinity")
# Iterate over each variable
for (var in vars) {
# Create the formula for the linear model
formula_chl <- paste("chl ~", var)
formula_phy <- paste("phy ~", var)
# Fit the linear model for 'chl'
lm_chl <- lm(formula_chl, data = ag_df_CR)
# Fit the linear model for 'phy'
lm_phy <- lm(formula_phy, data = ag_df_CR)
# Store the results in the list
lm_list[[paste(var, "chl", sep = "_")]] <- lm_chl
lm_list[[paste(var, "phy", sep = "_")]] <- lm_phy
}
```
```{r}
# Create an empty data frame to store the results
results_df <- data.frame(variable = character(), response = character(),
correlation = numeric(), r_squared = numeric(), p_value = numeric(),
stringsAsFactors = FALSE)
# Iterate over each variable
for (var in vars) {
# Create the formula for the linear model
formula_chl <- paste("chl ~", var)
formula_phy <- paste("phy ~", var)
# Fit the linear model for 'chl'
lm_chl <- lm(formula_chl, data = ag_df_CR)
# Fit the linear model for 'phy'
lm_phy <- lm(formula_phy, data = ag_df_CR)
# Extract the correlation coefficient, R-squared value, and p-value
cor_chl <- cor(ag_df_CR[[var]], ag_df_CR$chl)
cor_phy <- cor(ag_df_CR[[var]], ag_df_CR$phy)
r_squared_chl <- summary(lm_chl)$r.squared
r_squared_phy <- summary(lm_phy)$r.squared
p_value_chl <- summary(lm_chl)$coefficients[2, 4]
p_value_phy <- summary(lm_phy)$coefficients[2, 4]
# Add the results to the data frame
results_df <- rbind(results_df,
data.frame(variable = var, response = "chl",
correlation = cor_chl, r_squared = r_squared_chl, p_value = p_value_chl),
data.frame(variable = var, response = "phy",
correlation = cor_phy, r_squared = r_squared_phy, p_value = p_value_phy))
}
# Print the results
print(results_df)
```
#List of all data
```{r}
#CR BUOY
ag_df_CR
ag_df_CR$chl
ag_df_CR$phy
ag_df_CR$ph
ag_df_CR$Celsius
ag_df_CR$spcond
ag_df_CR$do
ag_df_CR$turb
ag_df_CR$Salinity
#ag_Logan
ag_Logan$HourlyAltimeterSetting
ag_Logan$HourlyDewPointTemperature
ag_Logan$HourlyDryBulbTemperature
ag_Logan$HourlyPrecipitation
ag_Logan$HourlyRelativeHumidity
ag_Logan$HourlyStationPressure
ag_Logan$HourlyVisibility
ag_Logan$HourlyWetBulbTemperature
ag_Logan$HourlyWindDirection
ag_Logan$HourlyWindGustSpeed
ag_Logan$HourlyWindSpeed
#CAM017
CAM017$MG
#bacteria
ag_CR_bacteria$Enterococcus
ag_CR_bacteria$E_coli
ag_CR_nutrients$`Ammonium (uM)`
#nutrients
ag_CR_nutrients$`Nitrate+nitrite (uM)`
ag_CR_nutrients$`Phosphate (uM)`
ag_CR_nutrients$`Phosphate (uM)`
ag_CR_nutrients$`Total phosphorus (uM)`
ag_CR_nutrients$`Chlorophyll a (ug/L)`
ag_CR_nutrients$`Phaeophytin (ug/L)`
#aggregated
All.ag_Logan
All.CR_nutrients
All.CR_bacteria
All.Var
```
###9:12
```{r}
morndf_CR
morndf_CR$chl
mornddf_CR$phy
mornddf_CR$ph
mornddf_CR$Celsius
mornddf_CR$spcond
mornddf_CR$do
mornddf_CR$turb
mornddf_CR$Salinity
```
###HOURLY
```{r}
df_CR
df_CR$chl
df_CR$phy
df_CR$ph
df_CR$Celsius
df_CR$spcond
df_CR$do
df_CR$turb
df_CR$Salinity
```
##LOGAN
```{r}
ag_Logan$HourlyAltimeterSetting
ag_Logan$HourlyDewPointTemperature
ag_Logan$HourlyDryBulbTemperature
ag_Logan$HourlyPrecipitation
ag_Logan$HourlyRelativeHumidity
ag_Logan$HourlyStationPressure
ag_Logan$HourlyVisibility
ag_Logan$HourlyWetBulbTemperature
ag_Logan$HourlyWindDirection
ag_Logan$HourlyWindGustSpeed
ag_Logan$HourlyWindSpeed
```
##MWRA Non-CSO
```{r}
#ag_CR_secchi
ag_CR_secchi$`Total Suspended Solids (mg/L)`
#ag_CR_nutrients
ag_CR_nutrients$`Ammonium (uM)`
ag_CR_nutrients$`Nitrate+nitrite (uM)`
ag_CR_nutrients$`Phosphate (uM)`
ag_CR_nutrients$`Total phosphorus (uM)`
ag_CR_nutrients$`Phaeophytin (ug/L)`
#ag_CR_bacteria
ag_CR_bacteria$Enterococcus
ag_CR_bacteria$E_coli
ag_CR_bacteria$coliform
```
##CSO
```{r}
CAM017$MG
```
```{r}
All.CR_bacteria
All.CR_secchi
All.CR_nutrients
```
#Vignette
```{r}
vignette('rEDM-tutorial')
```
###LOAD DATA
##CR BUOY
```{r}
#CR2023
if (file.exists("CR2023.rds")) {
# If it's saved locally, load the data
CR2023 <- readRDS("CR2023.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR2023 <- read_sheet('https://docs.google.com/spreadsheets/d/1sMKGzW71qErP8DIrxWDvlgqN3o9MrprLjjAFGsOie3I/edit?usp=sharing')
saveRDS(CR2023, "CR2023.rds")
}
#CR2022
if (file.exists("CR2022.rds")) {
# If it's saved locally, load the data
CR2022 <- readRDS("CR2022.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR2022 <- read_sheet('https://docs.google.com/spreadsheets/d/1F5l-1dWi7EFOaEKf9o3eUBi6BgQOrC1Rj_lVNRPrx5U/edit?usp=sharing', sheet = 2)
saveRDS(CR2022, "CR2022.rds")
}
#CR2021
if (file.exists("CR2021.rds")) {
# If it's saved locally, load the data
CR2021 <- readRDS("CR2021.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR2021 <- read_sheet('https://docs.google.com/spreadsheets/d/1vaDoymaAQmNFKqIgo1LStI7GQSKDeIqLy7We85IA3Oo/edit?usp=sharing', sheet = 2)
saveRDS(CR2021, "CR2021.rds")
}
####FIX 2021
#CR2020
if (file.exists("CR2020.rds")) {
# If it's saved locally, load the data
CR2020 <- readRDS("CR2020.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR2020 <- read_sheet('https://docs.google.com/spreadsheets/d/1Qvt0PIK4wPFPKbkM98pSQp9TbPsu_a6frTWFKv3aNQ4/edit?usp=sharing', sheet = 2)
saveRDS(CR2020, "CR2020.rds")
}
#CR2019
if (file.exists("CR2019.rds")) {
# If it's saved locally, load the data
CR2019 <- readRDS("CR2019.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR2019 <- read_sheet('https://docs.google.com/spreadsheets/d/1PGZCChA-8HU-cLvPSxqrxd89gLTletDYgogMGwv4Dg4/edit?usp=sharing')
saveRDS(CR2019, "CR2019.rds")
}
#CR2018
if (file.exists("CR2018.rds")) {
# If it's saved locally, load the data
CR2018 <- readRDS("CR2018.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR2018 <- read_sheet('https://docs.google.com/spreadsheets/d/1CO0DHuKb3C-hE9z-r5yR-SXwPnZWZg4vQDuEtRv2r7k/edit?usp=sharing', sheet = 2)
saveRDS(CR2018, "CR2018.rds")
}
#CR2017
if (file.exists("CR2017.rds")) {
# If it's saved locally, load the data
CR2017 <- readRDS("CR2018.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR2017 <- read_sheet('https://docs.google.com/spreadsheets/d/11SO-RxRhcc4qqn3gRt4n13JWClFoG--kISMoPAR2FRA/edit?usp=sharing', sheet = 2)
saveRDS(CR2017, "CR2017.rds")
}
#CR2016
if (file.exists("CR2016.rds")) {
# If it's saved locally, load the data
CR2016 <- readRDS("CR2016.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR2016 <- read_sheet('https://docs.google.com/spreadsheets/d/1sMcEOIzxPTUZ93-pTJBE0lQ3SI8kKihLZRdpZ32_Lyw/edit?usp=sharing', sheet = 2)
saveRDS(CR2017, "CR2016.rds")
}
#CR2015
if (file.exists("CR2015.rds")) {
# If it's saved locally, load the data
CR2015 <- readRDS("CR2015.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR2015 <- read_sheet('https://docs.google.com/spreadsheets/d/1t5joquxku9OfQGDdsabp-GdR2sUw8hPDcwatC3SL90Q/edit?usp=sharing', sheet = 2)
saveRDS(CR2015, "CR2015.rds")
}
```
#df_CR
```{r}
#bind rows
df_CR <- bind_rows(
CR2015,
CR2016,
CR2017,
CR2018,
CR2019,
CR2020,
CR2021,
CR2022,
CR2023
)
#combine data
df_CR <- df_CR %>%
mutate(hour = pmin(hour(`time est`),hour(`time edt`),na.rm=T),
minute = pmin(minute(`time est`),minute(`time edt`),na.rm=T)) %>%
select(date,hour,minute,chl=`chlorophyll (rfu)`,phy=`phycocyanin (rfu)`, ph=`ph`, Celsius =`temp c`, spcond = `spcond (ms/cm)`, do = `do (mg/l)`,turb = `turbidity (fnu)` )
#%>% rename(`time est`=time)
#Aggregate Values Daily
df_CR$spcond = df_CR$spcond * 0.1 #mS to S/m
df_CR$Salinity <-
swSCTp(
df_CR$spcond,
df_CR$Celsius,
pressure = rep(10.1325,NROW(df_CR)),
# "S/cm",
"",
eos = getOption("oceEOS", default = "gsw")
)
if (file.exists("df_CR.rds")) {
# If it's saved locally, load the data
df_CR <- readRDS("df_CR.rds")
} else {
saveRDS(df_CR, "df_CR.rds")
}
```
#ag_df_CR
```{r}
#ag_df_CR
ag_df_CR <-df_CR %>%
mutate(date = trunc(date,"days")) %>%
group_by(date) %>%
summarise(chl=mean(chl, na.rm = TRUE), phy=mean(phy, na.rm = TRUE), ph=mean(ph, na.rm = TRUE), Celsius=mean(Celsius,na.rm = TRUE), spcond=mean(spcond,na.rm = TRUE), do=mean(do,na.rm = TRUE), turb=mean(turb,na.rm = TRUE), Salinity = mean(Salinity,na.rm = TRUE))
## eventually find a way around this
ag_df_CR <- na.omit(ag_df_CR)
ag_df_CR <- ag_df_CR %>%
mutate(date = as.POSIXct(date, format = "%Y-%m-%d"))
if (file.exists("ag_df_CR.rds")) {
# If it's saved locally, load the data
ag_df_CR <- readRDS("ag_df_CR.rds")
} else {
saveRDS(ag_df_CR, "ag_df_CR.rds")
}
```
# 9-12 Values
```{r}
df_CR <- readRDS("df_CR.rds")
morndf_CR <- df_CR %>%
filter(hour %in% c(9, 10, 11))
morndf_CR <-morndf_CR %>%
mutate(date = trunc(date,"days")) %>%
group_by(date) %>%
summarise(chl=mean(chl, na.rm = TRUE), phy=mean(phy, na.rm = TRUE), ph=mean(ph, na.rm = TRUE), Celsius=mean(Celsius,na.rm = TRUE), spcond=mean(spcond,na.rm = TRUE), do=mean(do,na.rm = TRUE), turb=mean(turb,na.rm = TRUE), Salinity = mean(Salinity,na.rm = TRUE))
morndf_CR <- morndf_CR %>%
mutate(date = as.POSIXct(date, format = "%Y-%m-%d"))
if (file.exists("morndf_CR.rds")) {
# If it's saved locally, load the data
morndf_CR <- readRDS("morndf_CR.rds")
} else {
saveRDS(morndf_CR, "morndf_CR.rds")
}
```
###LOGAN
```{r}
Logan <- read_sheet('https://docs.google.com/spreadsheets/d/1h3mNXsGZxB4fhkZQGr2e8z6CxAQtumA5a208-GEYvBw/edit?usp=sharing')
# Convert to POSIXct object
Logan$DATE <- as.POSIXct(Logan$DATE, format = "%Y-%m-%dT%H:%M:%S")
Logan2 <- Logan
# Remove "s" suffixes from HourlyAltimeterSetting column
Logan2$HourlyAltimeterSetting <- gsub("s$", "", Logan2$HourlyAltimeterSetting)
Logan2$HourlySeaLevelPressure <- gsub("s$", "", Logan2$HourlySeaLevelPressure)
Logan2$HourlyDewPointTemperature <- gsub("s$", "", Logan2$HourlyDewPointTemperature)
Logan2$HourlyPrecipitation <- gsub("T", "0", Logan2$HourlyPrecipitation)
Logan2 <- Logan2 %>%
mutate_at(vars(HourlyAltimeterSetting, HourlyDewPointTemperature, HourlyDryBulbTemperature,
HourlyPrecipitation, HourlyPressureChange, HourlyPressureTendency,
HourlyRelativeHumidity, HourlySeaLevelPressure, HourlyStationPressure,
HourlyVisibility, HourlyWetBulbTemperature, HourlyWindDirection,
HourlyWindGustSpeed, HourlyWindSpeed), ~ifelse(. == "NULL", NA, .))
# Convert columns to numeric (excluding DATE)
numeric_cols <- c("HourlyAltimeterSetting", "HourlyDewPointTemperature", "HourlyDryBulbTemperature",
"HourlyPrecipitation", "HourlyPressureChange", "HourlyPressureTendency",
"HourlyRelativeHumidity", "HourlySeaLevelPressure", "HourlyStationPressure",
"HourlyVisibility", "HourlyWetBulbTemperature", "HourlyWindDirection",
"HourlyWindGustSpeed", "HourlyWindSpeed")
Logan2 <- Logan2 %>%
mutate(across(all_of(numeric_cols), as.numeric))
ag_Logan <- Logan2 %>%
group_by(DATE) %>%
summarise(
HourlyAltimeterSetting = mean(HourlyAltimeterSetting, na.rm = TRUE),
HourlyDewPointTemperature = mean(HourlyDewPointTemperature, na.rm = TRUE),
HourlyDryBulbTemperature = mean(HourlyDryBulbTemperature, na.rm = TRUE),
HourlyPrecipitation = sum(HourlyPrecipitation, na.rm = TRUE),
# HourlyPressureChange = mean(HourlyPressureChange, na.rm = TRUE), SKIP
# HourlyPressureTendency = mean(HourlyPressureTendency, na.rm = TRUE), SKIP
HourlyRelativeHumidity = mean(HourlyRelativeHumidity, na.rm = TRUE),
# HourlySeaLevelPressure = mean(HourlySeaLevelPressure, na.rm = TRUE), SKIP
HourlyStationPressure = mean(HourlyStationPressure, na.rm = TRUE),
HourlyVisibility = mean(HourlyVisibility, na.rm = TRUE),
HourlyWetBulbTemperature = mean(HourlyWetBulbTemperature, na.rm = TRUE),
HourlyWindDirection = mean(HourlyWindDirection, na.rm = TRUE),
HourlyWindGustSpeed = mean(HourlyWindGustSpeed, na.rm = TRUE),
HourlyWindSpeed = mean(HourlyWindSpeed, na.rm = TRUE)
)
mutate(date = trunc(date,"days")) %>%
# Convert to POSIXct object
ag_Logan$DATE <- as.POSIXct(ag_Logan$DATE, format = "%Y-%m-%dT%H:%M:%S")
ag_Logan <- ag_Logan %>%
mutate(DATE = as.Date(DATE)) %>%
group_by(DATE) %>%
summarise(
HourlyAltimeterSetting = mean(HourlyAltimeterSetting, na.rm = TRUE),
HourlyDewPointTemperature = mean(HourlyDewPointTemperature, na.rm = TRUE),
HourlyDryBulbTemperature = mean(HourlyDryBulbTemperature, na.rm = TRUE),
HourlyPrecipitation = sum(HourlyPrecipitation, na.rm = TRUE),
# HourlyPressureChange = mean(HourlyPressureChange, na.rm = TRUE), SKIP
# HourlyPressureTendency = mean(HourlyPressureTendency, na.rm = TRUE), SKIP
HourlyRelativeHumidity = mean(HourlyRelativeHumidity, na.rm = TRUE),
# HourlySeaLevelPressure = mean(HourlySeaLevelPressure, na.rm = TRUE), SKIP
HourlyStationPressure = mean(HourlyStationPressure, na.rm = TRUE),
HourlyVisibility = mean(HourlyVisibility, na.rm = TRUE),
HourlyWetBulbTemperature = mean(HourlyWetBulbTemperature, na.rm = TRUE),
HourlyWindDirection = mean(HourlyWindDirection, na.rm = TRUE),
HourlyWindGustSpeed = mean(HourlyWindGustSpeed, na.rm = TRUE),
HourlyWindSpeed = mean(HourlyWindSpeed, na.rm = TRUE)
)
if (file.exists("ag_Logan.rds")) {
# If it's saved locally, load the data
ag_Logan <- readRDS("ag_Logan.rds")
} else {
saveRDS(ag_Logan, "ag_Logan.rds")
}
#ag_Logan <- na.omit(ag_Logan)
```
### MWRA Non-CSO
```{r}
#READ DATA IN
#CR_bacteria
if (file.exists("CR_bacteria.rds")) {
# If it's saved locally, load the data
CR_bacteria <- readRDS("CR_bacteria.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR_bacteria <- read_sheet('https://docs.google.com/spreadsheets/d/1V1DJc4er2gctuRN2bv3u3Db0bGxz566ec8j6xmXFZ70/edit?usp=sharing', range = 'cr_bacteria')
saveRDS(CR_bacteria, "CR_bacteria.rds")
}
#CR_secchi
if (file.exists("CR_secchi.rds")) {
# If it's saved locally, load the data
CR_secchi <- readRDS("CR_secchi.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR_secchi <- read_sheet('https://docs.google.com/spreadsheets/d/1fW4_60i-SYkdTPlFQQdN0b5nlZirc2uQyjDN_dvWA18/edit?usp=sharing', range = 'cr_secchi')
saveRDS(CR_secchi, "CR_secchi.rds")
}
#CR_physical
if (file.exists("CR_physical.rds")) {
# If it's saved locally, load the data
CR_physical <- readRDS("CR_physical.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR_physical <- read_sheet('https://docs.google.com/spreadsheets/d/1P5kzPHFABPTKffxM2yQBTIQWq9NAmnGd3PF6kOsSFNU/edit?usp=sharing', range = 'cr_physical')
saveRDS(CR_physical, "CR_physical.rds")
}
#CR_nutrients
if (file.exists("CR_nutrients.rds")) {
# If it's saved locally, load the data
CR_nutrients <- readRDS("CR_nutrients.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
CR_nutrients <- read_sheet('https://docs.google.com/spreadsheets/d/1kPHmgJUHF7GtNJJDme_gWgB7hTS7i6SE7pqazChKMZ4/edit?usp=sharing')
saveRDS(CR_physical, "CR_nutrients.rds")
}
```
```{r}
#NEW NAMES
## CRBacteria
Bacteria_new_names <- c(
"Project_ID",
"Region",
"Subregion",
"DEP_Segment",
"Station ID",
"Date",
"Depth_category",
"Sample_depth",
"Enterococcus",
"Enterococcus_condition",
"E_coli",
"E_coli_condition",
"coliform",
"coliform_condition",
"Rainfall_1d",
"Rainfall_2d",
"Rainfall_3d"
)
#CRSECCHI
Secchi_new_names <- c(
"Project ID",
"Region",
"Subregion",
"DEP Segment",
"Station ID",
"Surface or Bottom",
"Depth to bottom (m)",
"Sample depth for TSS (m)",
"Date",
"Total Suspended Solids (mg/L)",
"Total Suspended Solids '<' or '>'",
"Secchi disk depth (m)"
)
#CRPHYSICAL
Physical_new_names <- c(
"Project ID",
"Region",
"Subregion",
"DEP segment",
"Station ID",
"Surface or Bottom",
"Date",
"Depth of measurement (m)",
"Temperature (C)",
"Salinity (PSU)",
"Specific Conductance (mS/cm)",
"Dissolved Oxygen (mg/L)",
"DO Pct Saturation (%)",
"pH",
"Turbidity (NTU)")
#CRNUTRIENTS
Nutrients_new_names <- c(
"Project ID",
"Region",
"Subregion",
"DEP segment",
"Station ID",
"Date",
"Surface or Bottom",
"Sample depth (m)",
"Ammonium (uM)",
"Ammonium '<' or '>'",
"Nitrate+nitrite (uM)",
"Nitrate+nitrite '<' or '>'",
"Total dissolved N (uM)",
"Total dissolved N '<' or '>'",
"Particulate nitrogen (uM)",
"Particulate nitrogen '<' or '>'",
"Total Kjeldahl Nitrogen (uM)",
"TKN '<' or '>'",
"Phosphate (uM)",
"Phosphate '<' or '>'",
"Total dissolved P (uM)",
"Total dissolved P '<' or '>'",
"Particulate P (uM)",
"Particulate P '<' or '>'",
"Total phosphorus (uM)",
"Total phosphorus '<' or '>'",
"Particulate carbon (uM)",
"Particulate carbon '<' or '>'",
"Chlorophyll a (ug/L)",
"Chlorophyll '<' or '>'",
"Phaeophytin (ug/L)",
"Phaeophytin '<' or '>'"
)
#Initial Data Wrangle
#CRBACTERIA
#colnames(CR_bacteria) <- as.character(CR_bacteria[5,]) #set header
#CR_bacteria <- CR_bacteria[-c(1:5), ] #Remove 1st 5 Rows
#rename columns
CR_bacteria <- CR_bacteria %>%
set_names(Bacteria_new_names) %>%
mutate(Date = map_vec(Date,as.Date))
#CR SECCHI
#colnames(CR_secchi) <- as.character(CR_secchi[5,]) #set header
#CR_secchi <- CR_secchi[-c(1:5), ] #Remove 1st 5 Rows
#rename columns
CR_secchi <- CR_secchi %>%
set_names(Secchi_new_names) %>%
mutate(Date = map_vec(Date,as.Date))
#CRPHYSICAL
#colnames(CR_physical) <- as.character(CR_physical[5,]) #set header
#CR_physical <- CR_physical[-c(1:5), ] #Remove 1st 5 Rows
#rename columns
CR_physical <- CR_physical %>%
set_names(Physical_new_names) %>%
mutate(Date = map_vec(Date,as.Date))
#CRNUTRIENTS
#colnames(CR_nutrients) <- as.character(CR_nutrients[5,]) #set header
#CR_nutrients <- CR_nutrients[-c(1:5), ] #Remove 1st 5 Rows
#rename columns
CR_nutrients <- CR_nutrients %>%
set_names(Nutrients_new_names) %>%
mutate(Date = map_vec(Date,as.Date))
```
```{r}
#SITE SELECTION
#CR_bacteria
CR_bacteria_011 <- subset(CR_bacteria, CR_bacteria$`Station ID` == "011")
#CR_nutrients
CR_nutrients_166 <- subset(CR_nutrients, CR_nutrients$`Station ID` == "166")
#CR_physical
CR_physical_011 <- subset(CR_physical, CR_physical$`Station ID` == "011")
#CR_secchi
CR_secchi_011 <- subset(CR_secchi, CR_secchi$`Station ID` == "011")
#AGGREGATE DAILY
#CR_bacteria
ag_CR_bacteria <-CR_bacteria_011 %>%
group_by(Date) %>%
summarise(Enterococcus = mean(Enterococcus, na.rm = TRUE), E_coli = mean(E_coli, na.rm = TRUE),)
ag_CR_bacteria <- na.omit(ag_CR_bacteria)
if (file.exists("ag_CR_bacteria.rds")) {
# If it's saved locally, load the data
ag_CR_bacteria <- readRDS("ag_CR_bacteria.rds")
} else {
saveRDS(ag_CR_bacteria, "ag_CR_bacteria.rds")
}
ag_CR_bacteria <- na.omit(ag_CR_bacteria)
#CR_nutrients
ag_CR_nutrients <- CR_nutrients_166 %>%
group_by(Date) %>%
summarise(`Ammonium (uM)` = mean(`Ammonium (uM)`, na.rm = TRUE), `Nitrate+nitrite (uM)` = mean(`Nitrate+nitrite (uM)`, na.rm = TRUE), `Phosphate (uM)` = mean(`Phosphate (uM)`, na.rm = TRUE), `Total phosphorus (uM)` = mean(`Total phosphorus (uM)`,na.rm = TRUE), `Chlorophyll a (ug/L)` = mean(`Chlorophyll a (ug/L)`,na.rm = TRUE), `Phaeophytin (ug/L)` = mean(`Phaeophytin (ug/L)`,na.rm = TRUE))
if (file.exists("ag_CR_nutrients.rds")) {
# If it's saved locally, load the data
ag_CR_nutrients <- readRDS("ag_CR_nutrients.rds")
} else {
saveRDS(ag_CR_nutrients, "ag_CR_bacteria.rds")
}
ag_CR_nutrients <- na.omit(ag_CR_nutrients)
#CR_physical
ag_CR_physical <- CR_physical_011 %>%
group_by(Date) %>%
summarise(`Temperature (C)` = mean(`Temperature (C)`, na.rm = TRUE),
`Salinity (PSU)` = mean(`Salinity (PSU)`, na.rm = TRUE),
`Specific Conductance (mS/cm)` = mean(`Specific Conductance (mS/cm)`, na.rm = TRUE),
`Dissolved Oxygen (mg/L)` = mean(`Dissolved Oxygen (mg/L)`, na.rm = TRUE),
`DO Pct Saturation (%)` = mean(`DO Pct Saturation (%)`, na.rm = TRUE),
`pH` = mean(`pH`, na.rm = TRUE),
`Turbidity (NTU)` = mean(`Turbidity (NTU)`, na.rm = TRUE))
if (file.exists("ag_CR_physical.rds")) {
# If it's saved locally, load the data
ag_CR_physical<- readRDS("ag_CR_physical.rds")
} else {
saveRDS(ag_CR_physical, "ag_CR_physical.rds")
}
ag_CR_physical <- na.omit(ag_CR_physical)
#CR_secchi
ag_CR_secchi <-CR_secchi_011 %>%
group_by(Date) %>%
summarise(`Total Suspended Solids (mg/L)`= mean(`Total Suspended Solids (mg/L)`, na.rm = TRUE ), `Secchi disk depth (m)` = mean(`Secchi disk depth (m)`, na.rm = TRUE))
if (file.exists("ag_CR_secchi.rds")) {
# If it's saved locally, load the data
ag_CR_secchil<- readRDS("ag_CR_secchi.rds")
} else {
saveRDS(ag_CR_secchi, "ag_CR_secchi.rds")
}
ag_CR_secchi <- na.omit(ag_CR_secchi)
```
###MWRA CSO
```{r}
#MWRACSOData2016
if (file.exists("MWRACSOData2016.rds")) {
# If it's saved locally, load the data
MWRACSOData2016 <- readRDS("MWRACSOData2016.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
MWRACSOData2016 <- read_sheet('https://docs.google.com/spreadsheets/d/1BF5lMyYrnILoS-rySwTGILiaqYfxA5-Pz5P2xUmOIq8/edit?usp=sharing')
saveRDS(MWRACSOData2016, "MWRACSOData2016.rds")
}
#MWRACSOData
if (file.exists("MWRACSOData.rds")) {
# If it's saved locally, load the data
MWRACSOData <- readRDS("MWRACSOData.rds")
} else {
# If it's not saved locally, read it from Google Sheets and save it locally
MWRACSOData <- read_sheet('https://docs.google.com/spreadsheets/d/1PRu1yvIuxOnMyazAVkXGv7dkOKUClNxl7CrvrctPREU/edit?usp=sharing')
saveRDS(MWRACSOData, "MWRACSOData.rds")
}
```
```{r}
#Extract Date
MWRACSOData$Date <- as.Date(MWRACSOData$`Start Time`, format = "%m/%d/%Y")
#Remove Excess Charecters
#Remove "Treated" in Volume Column
MWRACSOData$`Volume (MG)` <- gsub("Treated","",as.character(MWRACSOData$`Volume (MG)`))
#Remove "min" in Minutes Column
MWRACSOData$Minutes <- gsub("min","",as.character(MWRACSOData$Minutes))
#Remove Rows w/ "*"
MWRACSOData <- MWRACSOData %>% filter(!grepl("\\*", MWRACSOData$Duration))
#Replace NA w/ 0
MWRACSOData$Minutes[is.na(MWRACSOData$Minutes)] <- 0
#Make Duration Column
MWRACSOData$Minutes <- as.numeric(MWRACSOData$Minutes)
MWRACSOData$Hours <- as.numeric(MWRACSOData$Hours)
MWRACSOData$CombinedMinutes <- MWRACSOData$Minutes + (MWRACSOData$Hours * 60)
#Remove Columns
MWRACSOData <- MWRACSOData[-c(4:10)]
#Check Dates
unique_dates <- unique(MWRACSOData$Date)
# Sanity Check: CombinedMinutes to numeric and Date to Date format
MWRACSOData <- MWRACSOData %>%
mutate(CombinedMinutes = as.numeric(CombinedMinutes),
Date = as.Date(Date))
MWRACSOData <- MWRACSOData %>%
mutate(`Volume (MG)` = as.numeric(`Volume (MG)`),
Date = as.Date(Date))
# Group by Date and calculate the sum of CombinedMinutes