Convert a rectangular split to an R expression
Source:R/helpers_rectangular.R
rect_split_to_expr.RdThis function converts a split condition from a tree-based model into an
valid R expression. It is primarily used as a building block for
extract_rules() to construct paths to terminal nodes.
Examples
# Numeric comparison
rect_split_to_expr(list(column = "age", value = 25, operator = "<"))
#> age < 25
# Single character value uses ==
rect_split_to_expr(list(column = "color", value = "red", operator = "=="))
#> color == "red"
# Multiple character values use %in%
rect_split_to_expr(
list(column = "color", value = c("red", "blue"), operator = "%in%")
)
#> color %in% c("red", "blue")
# Evaluate the expression
expr <- rect_split_to_expr(list(column = "age", value = 30, operator = ">="))
test_data <- data.frame(age = c(20, 30, 40))
test_data[eval(expr, test_data), ]
#> [1] 30 40