join_node_attrs: Join new node attribute values using a data frame

Description Usage Arguments Value Examples

View source: R/join_node_attrs.R

Description

Join new node attribute values in a left join using a data frame. The use of a left join in this function allows for no possibility that nodes in the graph might be removed after the join.

Usage

1
join_node_attrs(graph, df, by_graph = NULL, by_df = NULL)

Arguments

graph

a graph object of class dgr_graph.

df

the data frame to use for joining.

by_graph

optional specification of the column in the graph's internal node data frame for the left join. If both by_graph and by_df are not provided, then a natural join will occur if there are columns in the graph's ndf and in df with identical names.

by_df

optional specification of the column in df for the left join. If both by_graph and by_df are not provided, then a natural join will occur if there are columns in the graph's ndf and in df with identical names. dgr_graph that is created using create_graph.

Value

a graph object of class dgr_graph.

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
47
48
49
# 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

DiagrammeR documentation built on May 30, 2017, 1:15 a.m.

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