showClass("WqData")
## Make a WqData object from sfbay data:
## Combine date and time into a POSIXct object called time.
## In this case, requires first making sure all sfbay times have 4
## digits. Also drop date, which is no longer needed.
sfb1 <- transform(sfbay, time = substring(10000 + time, 2, 5))
sfb1 <- transform(sfb1, time = paste(date, time, sep = ""))
sfb1 <- transform(sfb1, time = as.POSIXct(time, format = "%m/%d/%Y %H%M"))
sfb1$date <- NULL
## If time of day were not needed, then the following would suffice:
## sfb1 <- transform(sfbay, time = as.Date(date, "\%m/\%d/\%Y"))
## sfb1$date <- NULL
## 'Melt' the data into the correct format
## which automatically gives required variable and value fields.
require(reshape2)
m1 <- melt(sfb1, id.vars = c('time', 'stn', 'depth'))
## Make sure names of id.vars conform to WqData requirements
names(m1)[2] <- "site"
## Make sure all column classes conform to WqData requirements
m1 <- transform(m1,
site = factor(paste("s", site, sep = ""), ordered = TRUE)
)
## Remove NAs
m1 <- subset(m1, !is.na(value))
rownames(m1) <- 1:nrow(m1)
## Create the new WqData object
sfb <- new('WqData', m1)
## Summarize the data
summary(sfb)
## Create boxplot summary of data
plot(sfb, vars = c('chl', 'dox', 'spm'), num.col = 2)Run the code above in your browser using DataLab