recode_edge_attrs: Recode a set of edge attribute values

View source: R/recode_edge_attrs.R

recode_edge_attrsR Documentation

Recode a set of edge attribute values

Description

Within a graph's internal edge data frame (edf), recode character or numeric edge attribute values. Optionally, one can specify a replacement value for any unmatched mappings.

Usage

recode_edge_attrs(
  graph,
  edge_attr_from,
  ...,
  otherwise = NULL,
  edge_attr_to = NULL
)

Arguments

graph

A graph object of class dgr_graph.

edge_attr_from

The name of the edge attribute column from which values will be recoded.

...

Single-length character vectors with the recoding instructions. The first component should have the value to replace and the second should have the replacement value (in the form ⁠"[to_replace] -> [replacement]", ...⁠).

otherwise

An optional single value for recoding any unmatched values.

edge_attr_to

An optional name of a new edge attribute to which the recoded values will be applied. This will retain the original edge attribute and its values.

Value

A graph object of class dgr_graph.

See Also

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(), mutate_edge_attrs_ws(), 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()

Examples

# Create a random graph using the
# `add_gnm_graph()` function
graph <-
  create_graph() %>%
  add_gnm_graph(
    n = 4,
    m = 6,
    set_seed = 23) %>%
  set_edge_attrs(
    edge_attr = rel,
    values = c("a", "b", "a",
               "c", "b", "d"))

# Get the graph's internal edf
# to show which edge attributes
# are available
graph %>% get_edge_df()

# Recode the `rel` node
# attribute, creating a new edge
# attribute called `penwidth`;
# here, `a` is recoded to `1.0`,
# `b` maps to `1.5`, and all
# other values become `0.5`
graph <-
  graph %>%
  recode_edge_attrs(
    edge_attr_from = rel,
    "a -> 1.0",
    "b -> 1.5",
    otherwise = 0.5,
    edge_attr_to = penwidth)

# Get the graph's internal edf
# to show that the node
# attribute values had been
# recoded and copied into a
# new node attribute
graph %>% get_edge_df()


DiagrammeR documentation built on June 22, 2024, 11:21 a.m.