part_whole_relation {validate} | R Documentation |
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,
...
)
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 |
|
part |
|
aggregator |
|
tol |
|
by |
Name of variable, or |
... |
Extra arguments passed to aggregator (for example |
Value
A logical
vector of size length(value)
.
Examples
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)