node_count: Get count of all nodes or certain types of nodes

Description Usage Arguments Value Examples

Description

From a graph object of class dgr_graph, get a count of nodes in the graph and optionally obtain a count of nodes by their type.

Usage

1
node_count(graph, type = FALSE)

Arguments

graph

a graph object of class dgr_graph.

type

either a logical value, where TRUE provides a named vector of node count by type and FALSE (the default) provides a total count, or, a character vector of type values to filter the node count.

Value

a numeric vector of single length.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Set a seed
set.seed(24)

# Create a node data frame (ndf)
ndf <-
  create_node_df(
    n = 26,
    label = TRUE,
    type = c(rep("a", 7),
             rep("b", 9),
             rep("c", 8),
             rep("d", 2)))

# Create an edge data frame (edf)
edf <-
  create_edge_df(
    from = sample(1:26, replace = TRUE),
    to = sample(1:26, replace = TRUE),
    rel = c(rep("rel_a", 7),
            rep("rel_b", 9),
            rep("rel_c", 8),
            rep("rel_d", 2)))

# Create a graph using the ndf and edf
graph <-
  create_graph(
    nodes_df = ndf,
    edges_df = edf)

# Get counts of nodes grouped by the
# `type` attribute
node_count(graph, type = TRUE) # the default
#> a b c d
#> 7 9 8 2

# Get a total count of nodes with no grouping
node_count(graph, type = FALSE)
#> [1] 26

# Get a count of nodes of one or more
# specified types
node_count(graph, type = "a")
#> [1] 7

node_count(graph, type = c("a", "c"))
#> [1] 15


Search within the DiagrammeR package
Search all R packages, documentation and source code

Questions? Problems? Suggestions? or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.