After some help and a whole lot of pirating from other scripts I managed to piece together a script that will allow you to add non CDMO data to CDMO data and run them together through the SWMPr package to get an estimate of the ecosystem metabolism. As well as, have R spit out a csv of the combined WQ NUT MET data, and a csv for the Ecotab values.
#Reset R's Brain
rm(list=ls())
# load any installed packages
library(SWMPr)
library(ggplot2)
library(dplyr)
library(tidyr)
#Find out where R is looking for files
getwd()
#Set where R ~should~ look for files
#note the forward slashes - you may have trouble if you just copy and paste from windows explorer, as it may use backslashes
setwd("C:/Users/bourque_e/Desktop/Metab/WQ NUT MET for CDMO sites")
#Make sure R is now looking in the right place
getwd()
# import data for multiple stations
# this is an example path with hypothetical csv files
# change as needed for actual data
path <- 'C:/Users/bourque_e/Desktop/Metab/WQ NUT MET for CDMO sites'
# import your data. Copy the format of the CDMO WQ files and name differently then the CDMO ones.
mydat <- read.csv('C:/Users/bourque_e/Desktop/Metab/WQ NUT MET for CDMO sites/aapacpwq2000.csv', header = T)
mydat1 <- read.csv('C:/Users/bourque_e/Desktop/Metab/WQ NUT MET for CDMO sites/aapacpwq1999.csv', header = T)
mydat2 <- read.csv('C:/Users/bourque_e/Desktop/Metab/WQ NUT MET for CDMO sites/aapacpwq1998.csv', header = T)
## rBIND now or later????
# rbind
alldat <- rbind(mydat, mydat1, mydat2)
##
# process mydat to match swmpr format
# remove extra columns
alldat <- select(alldat, -StationCode, -isSWMP, -Historical, -ProvisionalPlus, -F_Record)
# format datetimestamp to correct class and timezone DateTimeStamp DTS must be capitolized!!!!!!!!!!!
alldat <- mutate(alldat,
DateTimeStamp = as.POSIXct(DateTimeStamp, format = '%m/%d/%Y %H:%M', tz = 'America/Jamaica')
)
# columns are case-sensitive, convert to lower
names(alldat) <- tolower(names(alldat))
# import
wq_dat <- import_local(path, 'apacpwq')
# combine swmp data with your data
# rbind
alldat <- rbind(alldat, wq_dat)
# make swmpr object
alldat <- swmpr(alldat, 'apacpwq')
## HAVE TO GET alldat into wq_dat
wq_dat<-alldat
# import
nut_dat <- import_local(path, 'apacpnut')
met_dat <- import_local(path, 'apaebmet')
dat <- comb(wq_dat, nut_dat, met_dat)
dat <- subset(qaqc(dat), rem_cols = T)
# combine datasets that come with SWMPr
# qaqc and subset to remove empty columns
# names of all the attributes
names(attributes(dat))
# examples of retrieving attributes
attr(dat, 'station')
attr(dat, 'parameters')
attr(dat, 'qaqc_cols')
attr(dat, 'date_rng')
attr(dat, 'timezone')
#to see first 6 rows of the subsets data
head(dat)
# Summary of whole dataset
summary(dat)
# individual variables
summary(dat$do_mgl)
# mean, range, var, etc.
# note use of na.rm
mean(dat$do_mgl, na.rm = T)
range(dat$do_mgl, na.rm = T)
var(dat$do_mgl, na.rm = T)
sd(dat$do_mgl, na.rm = T)
min(dat$do_mgl, na.rm = T)
max(dat$do_mgl, na.rm = T)
# how many missing values?
sum(is.na(dat$do_mgl))
## estimate metabolism
res <- ecometab(dat)
# write to csv file
write.csv(res, file = 'FingersCrossed13.csv', na="")
## plot summaries of the metabolism data
plot_metab(res)
#res1 prevents res from being reassociated with a different data set
res1 <- attr(res, 'metabolism')
#Write CSV in R without NA in every empty cell.
write.csv(res1, file = 'FingersCrossed13b.csv', na="")
# save the whole workspace as a .RData file
# will be saved in the working directory
save(list = ls(), file = 'my_workspace.RData')