From a graph object of class
dgr_graph
or a node data frame, set node
attribute properties for all nodes in the graph
using one of several whole-graph functions.
set_node_attr_w_fcn(graph, node_attr_fcn, ..., column_name = NULL)
graph | a graph object of class
|
---|---|
node_attr_fcn | the name of the function to
use for creating a column of node attribute values.
Valid functions are: |
... | arguments and values to pass to
the named function in |
column_name | an option to supply a column
name for the new node attribute column. If
|
either a graph object of class
dgr_graph
.
# Create a random graph using the # `add_gnm_graph()` function graph <- create_graph() %>% add_gnm_graph( n = 10, m = 22, set_seed = 23) %>% set_node_attrs( node_attr = value, values = rnorm( n = count_nodes(.), mean = 5, sd = 1) %>% round(1)) # Get the betweenness values for # each of the graph's nodes as a # node attribute graph_1 <- graph %>% set_node_attr_w_fcn( node_attr_fcn = "get_betweenness") # Inspect the graph's internal # node data frame graph_1 %>% get_node_df()#> id type label value betweenness__A #> 1 1 <NA> 1 4.4 9.333333 #> 2 2 <NA> 2 4.6 29.000000 #> 3 3 <NA> 3 5.9 19.166667 #> 4 4 <NA> 4 6.5 2.666667 #> 5 5 <NA> 5 4.1 0.500000 #> 6 6 <NA> 6 3.6 18.000000 #> 7 7 <NA> 7 5.4 12.000000 #> 8 8 <NA> 8 5.8 0.000000 #> 9 9 <NA> 9 4.7 10.333333 #> 10 10 <NA> 10 5.7 0.000000# If a specified function takes argument # values, these can be supplied as well graph_2 <- graph %>% set_node_attr_w_fcn( node_attr_fcn = "get_alpha_centrality", alpha = 2, exo = 2) # Inspect the graph's internal # node data frame graph_2 %>% get_node_df()#> id type label value alpha_centrality__A #> 1 1 <NA> 1 4.4 0.0621118 #> 2 2 <NA> 2 4.6 -0.5341615 #> 3 3 <NA> 3 5.9 -0.8157350 #> 4 4 <NA> 4 6.5 -0.6997930 #> 5 5 <NA> 5 4.1 1.0641822 #> 6 6 <NA> 6 3.6 -0.8737060 #> 7 7 <NA> 7 5.4 -0.6832298 #> 8 8 <NA> 8 5.8 0.9316770 #> 9 9 <NA> 9 4.7 -0.4679089 #> 10 10 <NA> 10 5.7 0.3685300# The new column name can be provided graph_3 <- graph %>% set_node_attr_w_fcn( node_attr_fcn = "get_pagerank", column_name = "pagerank") # Inspect the graph's internal # node data frame graph_3 %>% get_node_df()#> id type label value pagerank #> 1 1 <NA> 1 4.4 0.1416 #> 2 2 <NA> 2 4.6 0.1401 #> 3 3 <NA> 3 5.9 0.1262 #> 4 4 <NA> 4 6.5 0.0637 #> 5 5 <NA> 5 4.1 0.0478 #> 6 6 <NA> 6 3.6 0.1976 #> 7 7 <NA> 7 5.4 0.1318 #> 8 8 <NA> 8 5.8 0.0422 #> 9 9 <NA> 9 4.7 0.0693 #> 10 10 <NA> 10 5.7 0.0398# If `graph_3` is modified by # adding a new node then the column # `pagerank` will have stale data; we # can run the function again and re-use # the existing column name to provide # updated values graph_3 <- graph_3 %>% add_node( from = 1, to = 3) %>% set_node_attr_w_fcn( node_attr_fcn = "get_pagerank", column_name = "pagerank") # Inspect the graph's internal # node data frame graph_3 %>% get_node_df()#> id type label value pagerank #> 1 1 <NA> 1 4.4 0.1349 #> 2 2 <NA> 2 4.6 0.1352 #> 3 3 <NA> 3 5.9 0.1585 #> 4 4 <NA> 4 6.5 0.0670 #> 5 5 <NA> 5 4.1 0.0461 #> 6 6 <NA> 6 3.6 0.1300 #> 7 7 <NA> 7 5.4 0.1014 #> 8 8 <NA> 8 5.8 0.0400 #> 9 9 <NA> 9 4.7 0.0685 #> 10 10 <NA> 10 5.7 0.0440 #> 11 11 <NA> <NA> NA 0.0744