num_qual_neighbors: Test if neighborhood passes a logical filter

Description Usage Arguments Value Examples

View source: R/num_qual_neighbors.R

Description

Using the 'tidygraph' functions map_local_*(), it is easy to scan the neighborhood of all the nodes in a graph. This function takes a logical filter expression to apply on the node attributes of a graph, and returns the number of neighbors that qualify.

Usage

1
num_qual_neighbors(neighborhood, lgl_filter, ignore_nodes = c(), ...)

Arguments

neighborhood

passed by the tidygraph::map_local_lgl() function.

lgl_filter

an expression to apply to the node attributes of neighborhood; must be encapsulated in expr( ... )

ignore_nodes

nodes to exclude from the filter (use the "name" attribute of the nodes)

...

just takes any other arguments passed automatically by tidygraph::map_local_lgl()

Value

an integer of the number of nodes that qualify for each neighborhood

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
suppressPackageStartupMessages(library(tidygraph))
set.seed(0)
gr <- quick_barabasi(30)
gr

plot(gr)

# number of neighbors with a "B" in their name
B_gr <- gr %>%
    mutate(name_with_B = map_local_int(
        .f = num_qual_neighbors,
        lgl_filter = rlang::expr(stringr::str_detect(name, "B"))
    ))
B_gr %N>%
    filter(name_with_B > 0) %>%
    plot()

jhrcook/jhcutils documentation built on Sept. 2, 2020, 7:16 a.m.