Skip to content

Commit

Permalink
updates for USGS-R#36
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan S Read committed Oct 22, 2015
1 parent 599b871 commit 755f164
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 13 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 <jread@usgs.gov>
Description: tools for QAQCing water quality sensor data.
License: file LICENSE
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions R/clean.R
Original file line number Diff line number Diff line change
@@ -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)
}
}
13 changes: 4 additions & 9 deletions R/plot-sensor.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
15 changes: 15 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
```


41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|----------|:-------------------------------------------------------|
Expand Down Expand Up @@ -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
Expand All @@ -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
28 changes: 28 additions & 0 deletions man/clean.Rd
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 755f164

Please sign in to comment.