Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Welcome!

We’re so glad you’re thinking about contributing to an EPA open source project! If you’re unsure about anything, just ask — or submit your issue or pull request anyway. The worst that can happen is we’ll politely ask you to change something. We appreciate all friendly contributions.

We encourage you to read this project’s CONTRIBUTING policy (you are here), its
LICENSE, and its [README](https://github.com/USEPA/TADA/blob/develop/readme.md).

All contributions to this project will be released under the CCO dedication. By submitting a pull request or issue, you are agreeing to comply with this waiver of copyright interest.

If you have any questions or want to read more, check out the [EPA Open Source Project Repo](https://github.com/USEPA/open-source-projects) and [EPA's Interim Open Source Code Guidance](https://developer.epa.gov/guide/open-source-code/).
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: TADA
Type: Package
Title: Tools for Automated Data Assessment (TADA)
Title: Tools for Automated Data Assessment (TADA) R Package
Version: 0.0.1
Author: U.S. Environmental Protection Agency
Maintainer: Cristina Mullin <[email protected]>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion R/DataDiscoveryRetrieval.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ readWQPwebservice <- function(webservice) {
#' @return autocleaned TADA data profile
#'

autoclean <- function(.data, clean=TRUE){
autoclean <- function(.data){
# check that .data object is compatible with TADA
# check .data is of class data.frame
if("data.frame" %in% class(.data) == FALSE) {
Expand Down
21 changes: 12 additions & 9 deletions R/ResultFlagsIndependent.R
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,11 @@ BelowNationalWQXUpperThreshold <- function(.data, clean = TRUE){
#' Some organizations submit data for this field to indicate if the data
#' produced has an approved Quality Assurance Project Plan (QAPP) or not.
#' Y indicates yes, N indicates no. This function has two default inputs:
#' clean = TRUE, cleanNA = FALSE. The default removes rows of data where the
#' clean = TRUE and cleanNA = FALSE. The default removes rows of data where the
#' QAPPApprovedIndicator equals "N". Users could alternatively remove both N's
#' and NA's using the inputs clean = TRUE, cleanNA = TRUE. If both
#' and NA's using the inputs clean = TRUE and cleanNA = TRUE. If both
#' clean = FALSE and cleanNA = FALSE, the function will not make any changes to
#' the dataset.
#' the data.
#'
#' Note: This is not a required field, so it is often left blank (NA) even if
#' the data has an associated QAPP. All states and tribes that collect
Expand All @@ -503,12 +503,15 @@ BelowNationalWQXUpperThreshold <- function(.data, clean = TRUE){
#' submitted to WQP is NA.
#'
#' @param .data TADA dataframe
#' @param clean Two boolean arguments: 1) remove data where the
#' QAPPApprovedIndicator = "N" when clean = TRUE; 2) remove data where the
#' QAPPApprovedIndicator = NA when cleanNA = TRUE. Default is clean = TRUE and
#' cleanNA = FALSE.
#' @param cleanNA
#'
#' @param clean Boolean argument with two possible values called "TRUE" and
#' "FALSE". When clean=TRUE, rows of data where the QAPPApprovedIndicator equals
#' "N" will be removed. When, clean=FALSE, rows of data where the
#' QAPPApprovedIndicator equals "N" will be retained.
#' @param cleanNA Boolean argument with two possible values called "TRUE" and
#' "FALSE". When cleanNA=TRUE, rows of data where the QAPPApprovedIndicator
#' equals "NA" will be removed. When, cleanNA=FALSE, rows of data where the
#' the QAPPApprovedIndicator equals "NA" will be retained.
#'
#' @return When clean = FALSE and cleanNA = FALSE, no data is removed from the
#' dataset.
#'
Expand Down
107 changes: 59 additions & 48 deletions R/Transformations.R
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
#' Transform Units to WQX Target Units
#'
#' Function compares measure units in the input data to the Water Quality
#' Exchange (WQX) 3.0 QAQC Characteristic Validation table. Optional outputs
#' include: 1) the dataset with flag columns appended, indicating if data can be
#' converted, to which unit it can be converted, and the conversion factor
#' required to convert measure values, 2) the datset with units and result values
#' converted to the WQX target unit, or 3) converted result values and units with
#' flag columns appended, specifying if data can be converted. Default is
#' transform = TRUE and flag = TRUE.
#'
#' This function compares measure units in the input data to the Water Quality
#' Exchange (WQX) 3.0 QAQC Characteristic Validation table.
#'
#' This function will ALWAYS add the following two columns to the input dataset:
#' 1) "WQX.ResultMeasureValue.UnitConversion",
#' 2) "WQX.DetectionLimitMeasureValue.UnitConversion"
#' These two fields indicate if data can be converted."NoResultValue" means data
#' cannot be converted because there is no ResultMeasureValue, and "NoTargetUnit"
#' means data cannot be converted because the original unit is not associated with a target unit in WQX.
#' "Convert" means the data can be transformed, and "Converted" means that this function
#' has been run with the input transform = TRUE, and the values were already converted.
#'
#' It also uses the following six fields from the input dataset:
#' 1) "CharacteristicName",
#' 2) "ActivityMediaName",
#' 3) "ResultMeasureValue",
#' 4) "ResultMeasure.MeasureUnitCode",
#' 5) "DetectionQuantitationLimitMeasure.MeasureValue",
#' 6) "DetectionQuantitationLimitMeasure.MeasureUnitCode"
#'
#' This function adds the following two fields and transforms values within the
#' following four fields ONLY when transform=TRUE:
#' Adds: "ResultMeasureUnitCode.Original", and "DetectionLimitMeasureUnitCode.Original".
#' Transforms: "ResultMeasureValue", "ResultMeasure.MeasureUnitCode", "DetectionQuantitationLimitMeasure.MeasureValue", and "DetectionQuantitationLimitMeasure.MeasureUnitCode".
#'
#' This function adds the following two fields ONLY when transform=FALSE:
#' Adds: "WQX.ConversionFactor" and "WQX.TargetUnit".
#'
#' @param .data TADA dataset
#' @param transform Boolean argument; changes ResultMeasure.MeasureUnitCode and
#' DetectionQuantitationLimitMeasure.MeasureUnitCode to WQX target unit and
#' converts ResultMeasureValue and DetectionQuantitationLimitMeasure.MeasureValue
#' to corresponding target unit when transform = TRUE. Default is transform = TRUE.
#' @param flag Boolean argument; appends WQX.ResultMeasureValue.UnitConversion and
#' WQX.DetectionLimitMeasureValue.UnitConversion columns, indicating if data can be
#' converted. "Transform" means data can be converted, "NoResultValue" means data
#' cannot be converted because there is no ResultMeasureValue, and "NoTargetUnit"
#' means data cannot be converted because the original unit is not associated
#' with a target unit in WQX. Default is flag = TRUE.
#'
#' @param transform Boolean argument with two possible values, “TRUE” and “FALSE”.
#' Default is transform = TRUE.
#'
#' @return When transform = FALSE and flag = TRUE, flag columns are appended to
#' the dataset only. When transform = TRUE and flag = TRUE, flag columns are
#' appended to the dataset and unit conversions are executed. When transform = TRUE
#' and flag = FALSE, unit conversions are executed only. When transform = FALSE and
#' flag = FALSE, an error is returned (function would return the input dataframe
#' unchanged if input was allowed).
#' @return When transform=TRUE, result values and units are converted to WQX target units.
#' This function changes the values within the "ResultMeasure.MeasureUnitCode"
#' and "DetectionQuantitationLimitMeasure.MeasureUnitCode" to the WQX target units and
#' converts respective values within the "ResultMeasureValue" and "DetectionQuantitationLimitMeasure.MeasureValue"
#' fields. In addition to "WQX.ResultMeasureValue.UnitConversion"
#' and "WQX.DetectionLimitMeasureValue.UnitConversion", transform=TRUE will add
#' the following two fields to the input dataset, "ResultMeasureUnitCode.Original",
#' and "DetectionLimitMeasureUnitCode.Original", to retain the original result
#' and unit values.
#'
#' When transform = FALSE, result values and units are NOT converted to WQX target units,
#' but columns are appended to indicate what the target units and conversion factors are,
#' and if the data can be converted. In addition to "WQX.ResultMeasureValue.UnitConversion"
#' and "WQX.DetectionLimitMeasureValue.UnitConversion", transform=FALSE will add the
#' following two fields to the input dataset: "WQX.ConversionFactor" and "WQX.TargetUnit".
#'
#' @export

WQXTargetUnits <- function(.data, transform = TRUE, flag = TRUE){
WQXTargetUnits <- function(.data, transform = TRUE){

# check that .data object is compatible with TADA
# check .data is of class data.frame
Expand All @@ -49,14 +70,6 @@ WQXTargetUnits <- function(.data, transform = TRUE, flag = TRUE){
if(is.logical(transform) == FALSE){
stop("transform argument must be Boolean (TRUE or FALSE)")
}
# check if flag is boolean
if(is.logical(flag) == FALSE){
stop("flag argument must be Boolean (TRUE or FALSE)")
}
# check that both transform and flag do NOT equal FALSE
if(transform == FALSE & flag == FALSE){
stop("Both 'transform' and 'flag' arguments equal FALSE, which would return the input dataset unchanged. One or both arguments must be equal to TRUE.")
}

# execute function after checks are passed
if(all(c("CharacteristicName", "ActivityMediaName", "ResultMeasureValue",
Expand Down Expand Up @@ -102,8 +115,7 @@ WQXTargetUnits <- function(.data, transform = TRUE, flag = TRUE){
TRUE ~ WQX.ConversionFactor))
}

# add flag column when flag = TRUE
if(flag == TRUE) {

# add WQX.ResultMeasureValue.UnitConversion column
flag.data <- flag.data %>%
# apply function row by row
Expand All @@ -124,7 +136,6 @@ WQXTargetUnits <- function(.data, transform = TRUE, flag = TRUE){
!is.na(WQX.TargetUnit)) ~ as.character("Convert"),
is.na(DetectionQuantitationLimitMeasure.MeasureValue) ~ as.character("NoDetectionLimitValue"),
is.na(WQX.TargetUnit) ~ as.character("NoTargetUnit")))
}

if(transform == FALSE) {

Expand All @@ -148,7 +159,7 @@ WQXTargetUnits <- function(.data, transform = TRUE, flag = TRUE){
.after = "DetectionQuantitationLimitMeasure.MeasureUnitCode")


warning("Conversions required for range checks and TADATargetUnit conversions -- Unit conversions, data summaries, and data calculations may be affected.")
print("Conversions required for range checks and TADATargetUnit conversions -- Unit conversions, data summaries, and data calculations may be affected.")
return(flag.data)
}

Expand Down Expand Up @@ -200,7 +211,7 @@ WQXTargetUnits <- function(.data, transform = TRUE, flag = TRUE){
!is.na(WQX.TargetUnit) ~ WQX.TargetUnit,
is.na(WQX.TargetUnit) ~ DetectionQuantitationLimitMeasure.MeasureUnitCode))

if(flag == TRUE) {

# edit WQX.ResultMeasureValue.UnitConversion column
clean.data <- clean.data %>%
# apply function row by row
Expand All @@ -218,7 +229,7 @@ WQXTargetUnits <- function(.data, transform = TRUE, flag = TRUE){
dplyr::mutate(WQX.DetectionLimitMeasureValue.UnitConversion = dplyr::case_when(
(!is.na(DetectionQuantitationLimitMeasure.MeasureValue) & !is.na(WQX.TargetUnit)) ~ as.character("Converted"),
TRUE ~ WQX.DetectionLimitMeasureValue.UnitConversion))
}


# remove extraneous columns, fix field names
clean.data <- clean.data %>%
Expand All @@ -228,27 +239,27 @@ WQXTargetUnits <- function(.data, transform = TRUE, flag = TRUE){
# get .data column names
col.order <- colnames(.data)
# add ResultUnitConversion column to the list if flag = TRUE
if(flag == TRUE){
col.order <- append(col.order, c("WQX.ResultMeasureValue.UnitConversion",

col.order <- append(col.order, c("WQX.ResultMeasureValue.UnitConversion",
"WQX.DetectionLimitMeasureValue.UnitConversion"))
}

# add original units to list if transform = TRUE
if(transform == TRUE){
col.order <- append(col.order, c("ResultMeasureUnitCode.Original",
"DetectionLimitMeasureUnitCode.Original"))
}

# reorder columns in clean.data
clean.data <- clean.data[, col.order]
# place flag columns next to relevant fields if flag = TRUE
if(flag == TRUE){

# place flag columns next to relevant fields
clean.data <- clean.data %>%
dplyr::relocate("ResultMeasureUnitCode.Original",
.after = "ResultMeasure.MeasureUnitCode") %>%
dplyr::relocate("WQX.DetectionLimitMeasureValue.UnitConversion",
.after = "DetectionQuantitationLimitMeasure.MeasureUnitCode")
}

# Place original unit columns next to original columns
if(transform == TRUE){

clean.data <- clean.data %>%
dplyr::relocate("WQX.ResultMeasureValue.UnitConversion",
.after = "ResultMeasure.MeasureUnitCode") %>%
Expand Down
6 changes: 3 additions & 3 deletions R/Utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ RemoveEmptyColumns <- function(.data){
#'
#' for numeric data type
#'
#' @param .data TADA dataset
#' @param x Numeric data field from TADA profile
#'
#' @return Number of values to the right of the decimal point for numeric type data.
#'
Expand All @@ -87,12 +87,12 @@ decimalplaces <- function(x) {
#'
#' for character data type
#'
#' @param .data TADA dataset
#' @param x Numeric data field from TADA profile
#'
#' @return Number of values to the right of the decimal point for character type data.
#'
#'

decimalnumcount<-function(x){stopifnot(class(x)=="character")
x<-gsub("(.*)(\\.)|(*$)","",x)
nchar(x)}
nchar(x)}
19 changes: 11 additions & 8 deletions man/QAPPapproved.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading