diff --git a/DESCRIPTION b/DESCRIPTION index 67f2aeb..c54c4ef 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,9 +1,9 @@ Package: sensorQC Type: Package Title: sensorQC -Version: 0.3.2 -Date: 2015-10-21 -Author: Jordan S Read, Brad Garner, Brian Pellerin +Version: 0.3.3 +Date: 2015-10-22 +Author: Jordan S Read, Brad Garner, Brian Pellerin, Luke Loken Maintainer: Jordan S Read Description: tools for QAQCing water quality sensor data. License: file LICENSE diff --git a/NAMESPACE b/NAMESPACE index c85a0ca..bd0cb8b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,8 @@ S3method("[",sensor) S3method(calc_flags,sensor) +S3method(clean,numeric) +S3method(clean,sensor) S3method(flag,numeric) S3method(flag,sensor) S3method(flags,sensor) @@ -14,6 +16,7 @@ S3method(times,sensor) S3method(values,sensor) S3method(window,sensor) export(MAD) +export(clean) export(flag) export(flag.data.frame) export(persist) diff --git a/R/clean.R b/R/clean.R new file mode 100644 index 0000000..ad80dd1 --- /dev/null +++ b/R/clean.R @@ -0,0 +1,50 @@ +#' clean a sensor or a flagged sensor +#' +#' clean a sensor or a flagged sensor +#' +#' @param x sensor object +#' @param which flags to replace +#' @param \dots additional flags to use (passed to flag(x,...)) +#' @param replace replacement value for flagged indices. +#' NULL removes the flagged row +#' @rdname clean +#' +#' @examples +#' data = c(999999, 1,2,3,4,2,3,4) +#' sensor = flag(data, 'x > 9999') +#' clean(sensor) +#' clean(data, 'x > 9999', 'persist(x) > 10', 'MAD(x) > 3', replace=NA) +#' @export +clean = function(x, which, ..., replace){ + UseMethod('clean') +} + +#' @export +clean.numeric <- function(x, which = 'all', ..., replace=NULL){ + x = sensor(x) + clean(x, which = which, ..., replace=replace) +} + +#' @export +clean.sensor <- function(x, which = 'all', ..., replace=NULL){ + + if (which == 'all'){ + if (!is.null(x$flags)){ + flag.i = sort(unique(unlist(sapply(flags(x), function(x) x$flag.i)))) + x$flags <- NULL + } else { + return(x) + } + + if (is.null(replace)){ + x$sensor <- x$sensor[c(-flag.i),] + rownames(x$sensor) <- seq_len(nrow(x$sensor)) + } else { + x$sensor$x[flag.i] <- replace + } + return(x) + } else { + x <- flag(x, flag.defs=which, ...) + clean(x, which = 'all', replace=replace) + } +} \ No newline at end of file diff --git a/R/plot-sensor.R b/R/plot-sensor.R index d07a861..03fc76d 100644 --- a/R/plot-sensor.R +++ b/R/plot-sensor.R @@ -2,14 +2,9 @@ plot.sensor <- function(x, y=NULL, ...){ args = expand.grid(...) # check that ylab and xlab aren't in ... - plot.x = x$sensor$times - plot.y = x$sensor$x - if (!is.null(flags(x))){ - flag.i <- sort(unique(unlist(sapply(flags(x), function(x) x$flag.i)))) - plot.x = plot.x[-flag.i] - plot.y = plot.y[-flag.i] - - } - plot(plot.x, plot.y, ylab='sensor', xlab='DateTime') + x <- clean(x, which='all',replace=NULL) + + + plot(x$sensor$times, x$sensor$x, ylab='sensor', xlab='DateTime') } \ No newline at end of file diff --git a/README.Rmd b/README.Rmd index 729cf33..21f66aa 100644 --- a/README.Rmd +++ b/README.Rmd @@ -60,6 +60,7 @@ Use sensorQC with a simple vector of numbers: flag(c(3,2,4,3,3,4,2,4),'MAD(x) > 3') ``` +#### plotting data plot dataset w/ outliers: ```{r} plot(sensor) @@ -70,3 +71,17 @@ plot dataset w/o outliers: flagged = flag(sensor, 'x == 999999', 'persist(x) > 3', 'MAD(x,w) > 3', 'MAD(x) > 3') plot(flagged) ``` + +#### cleaning data +The `clean` function can be used to strip flagged data points from the record or replace them with other values (such as `NA` or -9999) +```{r} +data = c(999999, 1,2,3,4,2,3,4) +sensor = flag(data, 'x > 9999') +clean(sensor) +``` +or flag data and clean data all in one step: +```{r} +clean(data, 'x > 9999', 'persist(x) > 10', 'MAD(x) > 3', replace=NA) +``` + + diff --git a/README.md b/README.md index 197b707..a3dcabc 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ This package is still very much in development, so the API may change at any tim High-frequency aquatic sensor QAQC procedures. `sensorQC` imports data, and runs various statistical outlier detection techniques as specified by the user. -### `sensorQC` Functions (as of v0.3.2) +### `sensorQC` Functions (as of v0.3.3) | Function | Title | |----------|:-------------------------------------------------------| @@ -123,6 +123,8 @@ flag(c(3,2,4,3,3,4,2,4),'MAD(x) > 3') ## ## MAD(x) > 3 (0 flags) +#### plotting data + plot dataset w/ outliers: ``` r @@ -139,3 +141,40 @@ plot(flagged) ``` ![](README_files/figure-markdown_github/unnamed-chunk-7-1.png) + +#### cleaning data + +The `clean` function can be used to strip flagged data points from the record or replace them with other values (such as `NA` or -9999) + +``` r +data = c(999999, 1,2,3,4,2,3,4) +sensor = flag(data, 'x > 9999') +clean(sensor) +``` + + ## object of class "sensor" + ## x + ## 1 1 + ## 2 2 + ## 3 3 + ## 4 4 + ## 5 2 + ## 6 3 + ## 7 4 + +or flag data and clean data all in one step: + +``` r +clean(data, 'x > 9999', 'persist(x) > 10', 'MAD(x) > 3', replace=NA) +``` + + ## object of class "sensor" + ## x + ## 1 NA + ## 2 1 + ## 3 2 + ## 4 3 + ## 5 4 + ## 6 2 + ## 7 3 + ## 8 4 diff --git a/man/clean.Rd b/man/clean.Rd new file mode 100644 index 0000000..40c1d7a --- /dev/null +++ b/man/clean.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/clean.R +\name{clean} +\alias{clean} +\title{clean a sensor or a flagged sensor} +\usage{ +clean(x, which, ..., replace) +} +\arguments{ +\item{x}{sensor object} + +\item{which}{flags to replace} + +\item{replace}{replacement value for flagged indices. +NULL removes the flagged row} + +\item{\dots}{additional flags to use (passed to flag(x,...))} +} +\description{ +clean a sensor or a flagged sensor +} +\examples{ +data = c(999999, 1,2,3,4,2,3,4) +sensor = flag(data, 'x > 9999') +clean(sensor) +clean(data, 'x > 9999', 'persist(x) > 10', 'MAD(x) > 3', replace=NA) +} +