Select Git revision
binarise_graph_prop.R
-
Christian Hohenfeld authoredChristian Hohenfeld authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
binarise_graph_prop.R 1.31 KiB
#' Remove all connections that are not in the proportion of strongest edges
#'
#' Graph data is limited to connections at are in the proportion above the
#' specified threshold. Thus, specifying a threshold of 0.2 will filter out
#' the weakest 20% of connections.
#'
#' @param graph The graph to work on.
#' @param col The column in the edges that contains the weight.
#' @param threshold The proportional threshold to filter at.
#'
#' @return The graph with weights removed, containing only edges meeting
#' the threshold.
#'
#' @rdname binarise_graph_prop
#' @export
binarise_graph_prop <- function(graph, col, threshold) {
csym <- rlang::enquo(col)
abs_sym <- rlang::sym("abs_col")
pr_sym <- rlang::sym("pr")
graph_binary <- graph %>%
tidygraph::activate("edges") %>%
dplyr::mutate({{ abs_sym }} := abs({{ csym }})) %>%
dplyr::mutate({{ pr_sym }} := dplyr::percent_rank({{ abs_sym }})) %>%
dplyr::mutate(dplyr::across(
{{ pr_sym }}, function(x) ifelse({{ abs_sym }} == 1, 0, x))) %>%
dplyr::filter({{ pr_sym }} >= threshold) %>%
dplyr::select(-{{ abs_sym }}, -{{ pr_sym }}, -{{ csym }})
graph_binary
}
#' @rdname binarise_graph_prop
#' @export
binarize_graph_prop <- function(graph, col, threshold) {
binarise_graph_prop(graph, {{ col }}, threshold)
}