Generates daily stats (N, mean, min, max, range, std deviation) for the specified time period before a given date. Output is a multiple column CSV (Date and Parameter Name by statistic) and a report (HTML or DOCX) with plots. Input is the ouput file of the QC operation of ContDataQC().

PeriodStats(
  fun.myDate,
  fun.myDate.Format = NA,
  fun.myPeriod.N = 30,
  fun.myPeriod.Units = "d",
  fun.myFile,
  fun.myDir.import = getwd(),
  fun.myDir.export = getwd(),
  fun.myParam.Name,
  fun.myDateTime.Name = "Date.Time",
  fun.myDateTime.Format = NA,
  fun.myThreshold = NA,
  fun.myConfig = "",
  fun.myReport.format = "",
  fun.myReport.Dir = ""
)

Arguments

fun.myDate

Benchmark date.

fun.myDate.Format

Format of benchmark date. This should be the same format of the date in the data file. Default is %Y-%m-%d (e.g., 2017-12-31).

fun.myPeriod.N

Period length. Default = 30.

fun.myPeriod.Units

Period units (days or years written as d or y). Default is d.

fun.myFile

Filename (no directory) of data file. Must be CSV file.

fun.myDir.import

Directory for import data. Default is current working directory.

fun.myDir.export

Directory for export data. Default is current working directory.

fun.myParam.Name

Column name in myFile to perform summary statistics. One or two parameters can be specified.

fun.myDateTime.Name

Column name in myFile for date time. Default = "Date.Time".

fun.myDateTime.Format

Format of DateTime field. Default = %Y-%m-%d %H:%M:%S.

fun.myThreshold

Value to draw line on plot. For example, a regulatory limit. Default = NA

fun.myConfig

Configuration file to use for this data analysis. The default is always loaded first so only "new" values need to be included. This is the easiest way to control date and time formats.

fun.myReport.format

Report format (docx or html). Default is specified in config.R (docx).Can be customized in config.R; ContData.env$myReport.Format.

fun.myReport.Dir

Report (rmd) template folder. Default is the package rmd folder. Can be customized in config.R; ContData.env$myReport.Dir.

Value

Returns a csv with daily means and a PDF summary with plots into the specified export directory for the specified time period before the given date.

Details

The input is output file of the QC operation in ContDataQC(). That is, a file with Date.Time, and parameters (matching formats in config.R).

To get different periods (30, 60, or 90 days) change function input "fun.myPeriod.N". It is possible to provide a vector for Period.N and Period.Units. If the date range is longer than that in the data provided the stats will not calculate properly.

The dates must be in the standard format (Y-m-d) or the function may not work as intended. For example, the date is used in the file name and dates with "/" will result in an invalid file name.

One or two parameters can be analyzed at a time. If provide 2 parameters both will produce period statistic summaries. And the plots will have both parameters. The 2nd parameter will be on the 2nd (right) y-axis.

Requires doBy library for the period statistics summary and rmarkdown for the report.

Examples

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Save example files from Package to use for example
## This step not needed for users working on their own files
df.x <- DATA_period_test2_Aw_20130101_20141231
write.csv(df.x,"DATA_period_test2_Aw_20130101_20141231.csv")
myFile <- "config.ExcludeFailsFalse.R"
file.copy(file.path(path.package("ContDataQC"), "extdata", myFile)
          , file.path(getwd(), myFile))
#> [1] TRUE
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Load File to use for PeriodStats
myDir <- tempdir()
myFile <- "DATA_period_test2_AW_20130101_20141231.csv"
df.x <- read.csv(file.path(myDir, myFile))
#> Warning: cannot open file '/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpZn7gXz/DATA_period_test2_AW_20130101_20141231.csv': No such file or directory
#> Error in file(file, "rt"): cannot open the connection

# function inputs
myDate <- "2013-09-30"
myDate.Format <- "%Y-%m-%d"
myPeriod.N <- c(30, 60, 90, 120, 1)
myPeriod.Units <- c("d", "d", "d", "d", "y")
myFile <- "DATA_period_test2_Aw_20130101_20141231.csv"
myDir.import <- tempdir()
myDir.export <- tempdir()
myParam.Name <- "Water.Temp.C"
myDateTime.Name <- "Date.Time"
myDateTime.Format <- "%Y-%m-%d %H:%M:%S"
myThreshold <- 20
myConfig <- ""
myReport.format <- "docx"
# Custom Config
myConfig.Fail.Include  <- "config.ExcludeFailsFalse.R"

# Run Function
## Example 1. default report format (html)
PeriodStats(myDate
          , myDate.Format
          , myPeriod.N
          , myPeriod.Units
          , myFile
          , myDir.import
          , myDir.export
          , myParam.Name
          , myDateTime.Name
          , myDateTime.Format
          , myThreshold
          , myConfig)
#> Error in PeriodStats(myDate, myDate.Format, myPeriod.N, myPeriod.Units,     myFile, myDir.import, myDir.export, myParam.Name, myDateTime.Name,     myDateTime.Format, myThreshold, myConfig): Provided file (DATA_period_test2_Aw_20130101_20141231.csv) does not exist in the provided import directory (/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpZn7gXz).

## Example 2. DOCX report format
PeriodStats(myDate
          , myDate.Format
          , myPeriod.N
          , myPeriod.Units
          , myFile
          , myDir.import
          , myDir.export
          , myParam.Name
          , myDateTime.Name
          , myDateTime.Format
          , myThreshold
          , myConfig
          , myReport.format)
#> Error in PeriodStats(myDate, myDate.Format, myPeriod.N, myPeriod.Units,     myFile, myDir.import, myDir.export, myParam.Name, myDateTime.Name,     myDateTime.Format, myThreshold, myConfig, myReport.format): Provided file (DATA_period_test2_Aw_20130101_20141231.csv) does not exist in the provided import directory (/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpZn7gXz).

## Example 3. DOCX report format and Include Flag Failures
PeriodStats(myDate
          , myDate.Format
          , myPeriod.N
          , myPeriod.Units
          , myFile
          , myDir.import
          , myDir.export
          , myParam.Name
          , myDateTime.Name
          , myDateTime.Format
          , myThreshold
          , myConfig.Fail.Include
          , myReport.format)
#> Error in PeriodStats(myDate, myDate.Format, myPeriod.N, myPeriod.Units,     myFile, myDir.import, myDir.export, myParam.Name, myDateTime.Name,     myDateTime.Format, myThreshold, myConfig.Fail.Include, myReport.format): Provided file (DATA_period_test2_Aw_20130101_20141231.csv) does not exist in the provided import directory (/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpZn7gXz).

## Example 4. DOCX report format with two parameters
myParam.Name <- c("Water.Temp.C", "Sensor.Depth.ft")
PeriodStats(myDate
          , myDate.Format
          , myPeriod.N
          , myPeriod.Units
          , myFile
          , myDir.import
          , myDir.export
          , myParam.Name
          , myDateTime.Name
          , myDateTime.Format
          , myThreshold
          , myConfig
          , myReport.format)
#> Error in PeriodStats(myDate, myDate.Format, myPeriod.N, myPeriod.Units,     myFile, myDir.import, myDir.export, myParam.Name, myDateTime.Name,     myDateTime.Format, myThreshold, myConfig, myReport.format): Provided file (DATA_period_test2_Aw_20130101_20141231.csv) does not exist in the provided import directory (/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpZn7gXz).