Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

get_similar_nbrs

Get neighboring nodes based on node attribute similarity


Description

With a graph a single node serving as the starting point, get those nodes in a potential neighborhood of nodes (adjacent to the starting node) that have a common or similar (within threshold values) node attribute to the starting node.

Usage

get_similar_nbrs(graph, node, node_attr, tol_abs = NULL, tol_pct = NULL)

Arguments

graph

A graph object of class dgr_graph.

node

A single-length vector containing a node ID value.

node_attr

The name of the node attribute to use to compare with adjacent nodes.

tol_abs

If the values contained in the node attribute node_attr are numeric, one can optionally supply a numeric vector of length 2 that provides a lower and upper numeric bound as criteria for neighboring node similarity to the starting node.

tol_pct

If the values contained in the node attribute node_attr are numeric, one can optionally supply a numeric vector of length 2 that specifies lower and upper bounds as negative and positive percentage changes to the value of the starting node. These bounds serve as criteria for neighboring node similarity to the starting node.

Value

A vector of node ID values.

Examples

# Getting similar neighbors can
# be done through numerical comparisons;
# start by creating a random, directed
# graph with 18 nodes and 22 edges
# using the `add_gnm_graph()` function
graph <-
  create_graph() %>%
  add_gnm_graph(
    n = 18,
    m = 25,
    set_seed = 23) %>%
  set_node_attrs(
    node_attr = value,
    values = rnorm(
      n = count_nodes(.),
      mean = 5,
      sd = 1) %>% round(0))

# Starting with node `10`, we
# can test whether any nodes
# adjacent and beyond are
# numerically equivalent in `value`
graph %>%
  get_similar_nbrs(
    node = 10,
    node_attr = value)

# We can also set a tolerance
# for ascribing similarly by using
# either the `tol_abs` or `tol_pct`
# arguments (the first applies absolute
# lower and upper bounds from the
# value in the starting node and the
# latter uses a percentage difference
# to do the same); try setting `tol_abs`
# with a fairly large range to
# determine if several nodes can be
# selected
graph %>%
  get_similar_nbrs(
    node = 10,
    node_attr = value,
    tol_abs = c(1, 1))

# That resulted in a fairly large
# set of 4 neigboring nodes; for
# sake of example, setting the range
# to be very large will effectively
# return all nodes in the graph
# except for the starting node
graph %>%
  get_similar_nbrs(
    node = 10,
    node_attr = value,
    tol_abs = c(10, 10)) %>%
    length()

DiagrammeR

Graph/Network Visualization

v1.0.6.1
MIT + file LICENSE
Authors
Richard Iannone [aut, cre] (<https://orcid.org/0000-0003-3925-190X>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.