## Check that data is present for all quarters in 2018-2019
dat <- data.frame(
year = rep(c("2018","2019"),each=4)
, quarter = rep(sprintf("Q%d",1:4), 2)
, value = sample(20:50,8)
)
# Method 1: creating a data frame in-place (only for simple cases)
rule <- validator(contains_exactly(
expand.grid(year=c("2018","2019"), quarter=c("Q1","Q2","Q3","Q4"))
)
)
out <- confront(dat, rule)
values(out)
# Method 2: pass the keyset to 'confront', and reference it in the rule.
# this scales to larger key sets but it needs a 'contract' between the
# rule definition and how 'confront' is called.
keyset <- expand.grid(year=c("2018","2019"), quarter=c("Q1","Q2","Q3","Q4"))
rule <- validator(contains_exactly(all_keys))
out <- confront(dat, rule, ref=list(all_keys = keyset))
values(out)
## Globbing (use * as a wildcard)
# transaction data
transactions <- data.frame(
sender = c("S21", "X34", "S45","Z22")
, receiver = c("FG0", "FG2", "DF1","KK2")
, value = sample(70:100,4)
)
# forbidden combinations: if the sender starts with "S",
# the receiver can not start "FG"
forbidden <- data.frame(sender="S*",receiver = "FG*")
rule <- validator(does_not_contain(glob(forbidden_keys)))
out <- confront(transactions, rule, ref=list(forbidden_keys=forbidden))
values(out)
## Quick interactive testing
# use 'with':
with(transactions, does_not_contain(forbidden))
## Grouping
# data in 'long' format
dat <- expand.grid(
year = c("2018","2019")
, quarter = c("Q1","Q2","Q3","Q4")
, variable = c("import","export")
)
dat$value <- sample(50:100,nrow(dat))
periods <- expand.grid(
year = c("2018","2019")
, quarter = c("Q1","Q2","Q3","Q4")
)
rule <- validator(contains_exactly(all_periods, by=variable))
out <- confront(dat, rule, ref=list(all_periods=periods))
values(out)
# remove one export record
dat1 <- dat[-15,]
out1 <- confront(dat1, rule, ref=list(all_periods=periods))
values(out1)
values(out1)
Run the code above in your browser using DataLab