# Attributes
attr_1 <- c(30, 28, 40, 25, 25, 29, 27)
attr_2 <- c("M", "F", "U", "M", "F", "U", "M")
# A match criteria
## Example 1 - A maximum difference of 10 in attribute 1
s_cri1 <- sub_criteria(attr_1, match_funcs = range_match)
s_cri1
# Evaluate the match criteria
## Compare the first element of 'attr_1' against all other elements
eval_sub_criteria(s_cri1)
## Compare the second element of 'attr_1' against all other elements
x_pos_val <- seq_len(max(attr_eval(s_cri1)))
eval_sub_criteria(s_cri1,
x_pos = x_pos_val,
y_pos = rep(2, length(x_pos_val)))
## Example 2 - `s_cri1` AND an exact match on attribute 2
s_cri2 <- sub_criteria(
s_cri1,
sub_criteria(attr_2, match_funcs = exact_match),
operator = "and")
s_cri2
## Example 3 - `s_cri1` OR an exact match on attribute 2
s_cri3 <- sub_criteria(
s_cri1,
sub_criteria(attr_2, match_funcs = exact_match),
operator = "or")
s_cri3
# Evaluate the match criteria
eval_sub_criteria(s_cri2)
eval_sub_criteria(s_cri3)
# Alternatively, using `attr()`
AND_func <- function(x, y) range_match(x$a1, y$a1) & x$a2 == y$a2
OR_func <- function(x, y) range_match(x$a1, y$a1) | x$a2 == y$a2
## Create a match criteria
s_cri2b <- sub_criteria(attrs(.obj = list(a1 = attr_1, a2 = attr_2)),
match_funcs = AND_func)
s_cri3b <- sub_criteria(attrs(.obj = list(a1 = attr_1, a2 = attr_2)),
match_funcs = OR_func)
# Evaluate the match criteria
eval_sub_criteria(s_cri2b)
eval_sub_criteria(s_cri3b)
Run the code above in your browser using DataLab