Within a graph's internal edge data frame (edf), mutate numeric edge attribute values using one or more expressions.
mutate_edge_attrs(graph, ...)A graph object of class dgr_graph.
Expressions used for the mutation of edge attributes. LHS of each expression is either an existing or new edge attribute name. The RHS can consist of any valid R code that uses edge attributes as variables. Expressions are evaluated in the order provided, so, edge attributes created or modified are ready to use in subsequent expressions.
A graph object of class dgr_graph.
Other edge creation and removal:
add_edge(),
add_edge_clone(),
add_edge_df(),
add_edges_from_table(),
add_edges_w_string(),
add_forward_edges_ws(),
add_reverse_edges_ws(),
copy_edge_attrs(),
create_edge_df(),
delete_edge(),
delete_edges_ws(),
delete_loop_edges_ws(),
drop_edge_attrs(),
edge_data(),
join_edge_attrs(),
mutate_edge_attrs_ws(),
recode_edge_attrs(),
rename_edge_attrs(),
rescale_edge_attrs(),
rev_edge_dir(),
rev_edge_dir_ws(),
set_edge_attr_to_display(),
set_edge_attrs(),
set_edge_attrs_ws()
# Create a graph with 3 edges
graph <-
create_graph() %>%
add_path(n = 4) %>%
set_edge_attrs(
edge_attr = width,
values = c(3.4, 2.3, 7.2))
# Get the graph's internal edf
# to show which edge attributes
# are available
graph %>% get_edge_df()
#> id from to rel width
#> 1 1 1 2 <NA> 3.4
#> 2 2 2 3 <NA> 2.3
#> 3 3 3 4 <NA> 7.2
# Mutate the `width` edge
# attribute, dividing each
# value by 2
graph <-
graph %>%
mutate_edge_attrs(
width = width / 2)
# Get the graph's internal
# edf to show that the edge
# attribute `width` had its
# values changed
graph %>% get_edge_df()
#> id from to rel width
#> 1 1 1 2 <NA> 1.70
#> 2 2 2 3 <NA> 1.15
#> 3 3 3 4 <NA> 3.60
# Create a new edge attribute,
# called `length`, that is the
# log of values in `width` plus
# 2 (and, also, round all values
# to 2 decimal places)
graph <-
graph %>%
mutate_edge_attrs(
length = (log(width) + 2) %>%
round(2))
# Get the graph's internal edf
# to show that the edge attribute
# values had been mutated
graph %>% get_edge_df()
#> id from to rel width length
#> 1 1 1 2 <NA> 1.70 2.53
#> 2 2 2 3 <NA> 1.15 2.14
#> 3 3 3 4 <NA> 3.60 3.28
# Create a new edge attribute
# called `area`, which is the
# product of the `width` and
# `length` attributes
graph <-
graph %>%
mutate_edge_attrs(
area = width * length)
# Get the graph's internal edf
# to show that the edge attribute
# values had been multiplied
# together (with new attr `area`)
graph %>% get_edge_df()
#> id from to rel width length area
#> 1 1 1 2 <NA> 1.70 2.53 4.301
#> 2 2 2 3 <NA> 1.15 2.14 2.461
#> 3 3 3 4 <NA> 3.60 3.28 11.808