correctWithRules: Deterministic correction

Description Usage Arguments Value Details See Also Examples

Description

Apply simple replacement rules to a data.frame.

Usage

1
correctWithRules(rules, dat, strict = TRUE)

Arguments

rules

object of class correctionRules

dat

data.frame

strict

If TRUE, an error is produced when the rules use variables other than in the data.frame.

Value

list with altered data ($corrected) and a list of alterations ($corrections).

Details

This function applies the the rules one by one to dat and logs their actions. Rules are excuted in order of occurrence in the correctionRules so order may matter for the final result. Rules are applied to one record at the time, so the use of statistical funtions such as mean is useless, and forbidden by default. See correctionRules for details on the type of rules that are possible.

See Also

correctionRules

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## Some example data
dat <- data.frame(
   x = c(NA, 2, 0,-10),
   y = c(1, NA,NA, 6)
)

## a few rules
u <- correctionRules(expression(
   if ( is.na(x) ) x <- 0,
   if ( x == 0 && is.na(y) ) y <- 0,
   if ( is.na(y) ) y <- 1,
   if ( x < 0 ) y <- 0
))

correctWithRules(u,dat)

Example output

Loading required package: editrules
Loading required package: igraph

Attaching package: 'igraph'

The following objects are masked from 'package:stats':

    decompose, spectrum

The following object is masked from 'package:base':

    union


Attaching package: 'editrules'

The following objects are masked from 'package:igraph':

    blocks, normalize

$corrected
    x y
1   0 1
2   2 1
3   0 0
4 -10 0

$corrections
  row variable old new                            how
1   1        x  NA   0           if (is.na(x)) x <- 0
2   2        y  NA   1           if (is.na(y)) y <- 1
3   3        y  NA   0 if (x == 0 && is.na(y)) y <- 0
4   4        y   6   0              if (x < 0) y <- 0

deducorrect documentation built on May 2, 2019, 3:47 p.m.