Directors are functions that tell the partition algorithm what to try to reduce. as_director() is a helper function to create new directors to be used in partitioners. partitioners can be created with as_partitioner().

as_director(.pairs, .target, ...)

Arguments

.pairs

a function that returns a matrix of targets (e.g. a distance matrix of variables)

.target

a function that returns a vector of targets (e.g. the minimum pair)

...

Extra arguments passed to .f.

Value

a function to use in as_partitioner()

See also

Other directors: direct_distance(), direct_k_cluster()

Examples

# use euclidean distance to calculate distances
euc_dist <- function(.data) as.matrix(dist(t(.data)))

# find the pair with the minimum distance
min_dist <- function(.x) {
  indices <- arrayInd(which.min(.x), dim(as.matrix(.x)))

  #  get variable names with minimum distance
  c(
    colnames(.x)[indices[1]],
    colnames(.x)[indices[2]]
  )
}

as_director(euc_dist, min_dist)
#> function (.partition_step, ...) 
#> {
#>     if (ncol(.partition_step$reduced_data) == 1) {
#>         .partition_step$metric <- 0
#>         return(all_done(.partition_step))
#>     }
#>     pairwise <- .pairs(.partition_step$reduced_data, ...)
#>     pairwise <- tag_previous_targets(pairwise, .partition_step$target_history)
#>     if (all(is.na(pairwise))) {
#>         .partition_step$metric <- 0
#>         return(all_done(.partition_step))
#>     }
#>     .partition_step$target <- .target(pairwise)
#>     .partition_step$target_history <- add_history(.partition_step)
#>     .partition_step$last_target <- list(target = .partition_step$target)
#>     .partition_step
#> }
#> <bytecode: 0x555a658d51c0>
#> <environment: 0x555a658d6e78>