DiagrammeR (version 0.9.0)

join_edge_attrs: Join new edge attribute values using a data frame

Description

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

Usage

join_edge_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. dgr_graph that is created using create_graph.

by_graph

optional specification of the column in the graph's internal edge 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 edf 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 edf and in df with identical names.

Value

a graph object of class dgr_graph.

Examples

Run this code
# 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
# representing the graph edges (with `from` and `to`
# columns), and, a set of numeric values
set.seed(25)

df <-
  data.frame(
    from = c(1, 1, 2, 2, 3),
    to = c(2, 3, 4, 5, 5),
    values = rnorm(5, 5))

# Join the values in the data frame to the
# graph's edges; this works as a left join using
# identically-named columns in the graph and the df
# (in this case `from` and `to` are common to both)
graph <- graph %>% join_edge_attrs(df)

# Get the graph's internal edf to show that the
# join has been made
get_edge_df(graph)
#>   id from to  rel   values
#> 1  1    1  2 <NA> 4.788166
#> 2  2    1  3 <NA> 3.958409
#> 3  3    2  4 <NA> 3.846692
#> 4  4    2  5 <NA> 5.321531
#> 5  5    3  5 <NA> 3.499870
# }

Run the code above in your browser using DataCamp Workspace