# NOT RUN {
# Create a simple graph
graph <-
create_graph() %>%
add_n_nodes(5) %>%
add_edges_w_string("1->2 1->3 2->4 2->5 3->5")
# Create a data frame with node ID values and a
# set of numeric values
set.seed(25)
df <-
data.frame(
values = round(rnorm(6, 5), 2),
id = 1:6)
# Join the values in the data frame to the
# graph's nodes; this works as a left join using
# identically-named columns in the graph and the df
# (in this case the `id` column is common to both)
graph <- graph %>% join_node_attrs(df)
# Get the graph's internal ndf to show that the
# join has been made
get_node_df(graph)
#> id type label values
#> 1 1 <NA> <NA> 4.79
#> 2 2 <NA> <NA> 3.96
#> 3 3 <NA> <NA> 3.85
#> 4 4 <NA> <NA> 5.32
#> 5 5 <NA> <NA> 3.5
# Get betweenness values for each node and
# add them as a node attribute (Note the
# common column name `id` in the different
# tables results in a natural join)
graph <-
graph %>%
join_node_attrs(
get_betweenness(.))
# Get the graph's internal ndf to show that
# this join has been made
get_node_df(graph)
#> id type label values betweenness
#> 1 1 <NA> <NA> 4.79 2
#> 2 2 <NA> <NA> 3.96 7
#> 3 3 <NA> <NA> 3.85 1
#> 4 4 <NA> <NA> 5.32 0
#> 5 5 <NA> <NA> 3.50 2
# }
Run the code above in your browser using DataLab