/
get_degree_total.R
80 lines (75 loc) · 1.8 KB
/
get_degree_total.R
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#' Get total degree values for all nodes
#'
#' @description
#'
#' Get the total degree values for all nodes in a graph.
#'
#' @inheritParams render_graph
#' @param normalized set as `FALSE` (the default), the total degree will be
#' provided for each of the nodes (as a count of edges to and from each node).
#' When set as `TRUE`, then the result for each node will be divided by
#' the total number of nodes in the graph minus 1.
#'
#' @return A data frame with total degree values for each of the nodes.
#'
#' @examples
#' # Create a random graph using the
#' # `add_gnm_graph()` function
#' graph <-
#' create_graph(
#' directed = FALSE) %>%
#' add_gnm_graph(
#' n = 10,
#' m = 15,
#' set_seed = 23)
#'
#' # Get the total degree values
#' # for all nodes in the graph
#' graph %>%
#' get_degree_total()
#'
#' # Add the total degree values
#' # to the graph as a node
#' # attribute
#' graph <-
#' graph %>%
#' join_node_attrs(
#' df = get_degree_total(.))
#'
#' # Display the graph's
#' # node data frame
#' graph %>% get_node_df()
#'
#' @export
get_degree_total <- function(
graph,
normalized = FALSE
) {
# Validation: Graph object is valid
check_graph_valid(graph)
# Convert the graph to an igraph object
ig_graph <- to_igraph(graph)
# Get the total degree values for each of the
# graph's nodes
if (normalized) {
total_degree_values <-
igraph::degree(
ig_graph,
mode = "total",
normalized = TRUE)
}
if (!normalized) {
total_degree_values <-
igraph::degree(
ig_graph,
mode = "total",
normalized = FALSE)
}
# Create df with total degree scores
data.frame(
id = total_degree_values %>%
names() %>%
as.integer(),
total_degree = total_degree_values,
stringsAsFactors = FALSE)
}