Learn R Programming

validate (version 1.1.5)

part_whole_relation: Test whether details combine to a chosen aggregate

Description

Data in 'long' format often contain records representing totals (or other aggregates) as well as records that contain details that add up to the total. This function facilitates checking the part-whole relation in such cases.

Usage

part_whole_relation(
  values,
  labels,
  whole,
  part = NULL,
  aggregator = sum,
  tol = 1e-08,
  by = NULL,
  ...
)

Value

A logical vector of size length(value).

Arguments

values

A bare (unquoted) variable name holding the values to aggregate

labels

A bare (unquoted) variable name holding the labels indicating whether a value is an aggregate or a detail.

whole

[character] literal label or pattern recognizing a whole in labels. Use glob or rx to label as a globbing or regular expression pattern (see examples).

part

[character] vector of label values or pattern recognizing a part in labels. Use glob or rx to label as a globbing or regular expression pattern. When labeled with glob or rx, it must be a single string. If `part` is left unspecified, all values not recognized as an aggregate are interpreted as details that must be aggregated to the whole.

aggregator

[function] used to aggregate subsets of x. It should accept a numeric vector and return a single number.

tol

[numeric] tolerance for equality checking

by

Name of variable, or list of bare variable names, used to split the values and labels before computing the aggregates.

...

Extra arguments passed to aggregator (for example na.rm=TRUE).

Examples

Run this code
df <- data.frame(
   id = 10011:10020
 , period   = rep(c("2018Q1", "2018Q2", "2018Q3", "2018Q4","2018"),2)
 , direction = c(rep("import",5), rep("export", 5))
 , value     = c(1,2,3,4,10, 3,3,3,3,13)
)
## use 'rx' to interpret 'whole' as a regular expression.
rules <- validator(
  part_whole_relation(value, period, whole=rx("^\\d{4}$")
  , by=direction)
)

out <- confront(df, rules, key="id")
as.data.frame(out)

Run the code above in your browser using DataLab