part_whole_relation | R Documentation |
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.
part_whole_relation(
values,
labels,
whole,
part = NULL,
aggregator = sum,
tol = 1e-08,
by = NULL,
...
)
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 |
A logical
vector of size length(value)
.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.