Within a graph's internal node data frame (ndf), recode character or numeric node attribute values. Optionally, one can specify a replacement value for any unmatched mappings.
Arguments
- graph
A graph object of class
dgr_graph.- node_attr_from
The name of the node 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.
- node_attr_to
An optional name of a new node attribute to which the recoded values will be applied. This will retain the original node attribute and its values.
See also
Other node creation and removal:
add_n_node_clones(),
add_n_nodes(),
add_n_nodes_ws(),
add_node(),
add_node_clones_ws(),
add_node_df(),
add_nodes_from_df_cols(),
add_nodes_from_table(),
colorize_node_attrs(),
copy_node_attrs(),
create_node_df(),
delete_node(),
delete_nodes_ws(),
drop_node_attrs(),
join_node_attrs(),
layout_nodes_w_string(),
mutate_node_attrs(),
mutate_node_attrs_ws(),
node_data(),
rename_node_attrs(),
rescale_node_attrs(),
set_node_attr_to_display(),
set_node_attr_w_fcn(),
set_node_attrs(),
set_node_attrs_ws(),
set_node_position()
Examples
# Create a random graph using the
# `add_gnm_graph()` function
graph <-
create_graph() %>%
add_gnm_graph(
n = 5,
m = 10,
set_seed = 23) %>%
set_node_attrs(
node_attr = shape,
values =
c("circle", "hexagon",
"rectangle", "rectangle",
"circle"))
# Get the graph's internal ndf
# to show which node
# attributes are available
graph %>% get_node_df()
#> id type label shape
#> 1 1 <NA> 1 circle
#> 2 2 <NA> 2 hexagon
#> 3 3 <NA> 3 rectangle
#> 4 4 <NA> 4 rectangle
#> 5 5 <NA> 5 circle
# Recode the `shape` node
# attribute, so that `circle`
# is recoded to `square` and that
# `rectangle` becomes `triangle`
graph <-
graph %>%
recode_node_attrs(
node_attr_from = shape,
"circle -> square",
"rectangle -> triangle")
# Get the graph's internal
# ndf to show that the node
# attribute values had been recoded
graph %>% get_node_df()
#> id type label shape
#> 1 1 <NA> 1 square
#> 2 2 <NA> 2 hexagon
#> 3 3 <NA> 3 triangle
#> 4 4 <NA> 4 triangle
#> 5 5 <NA> 5 square
# Create a new node attribute,
# called `color`, that is based
# on a recoding of `shape`; here,
# map the square shape to a `red`
# color and map all other shapes
# to a `green` color
graph <-
graph %>%
recode_node_attrs(
node_attr_from = shape,
"square -> red",
otherwise = "green",
node_attr_to = color)
# Get the graph's internal ndf
# to see the change
graph %>% get_node_df()
#> id type label shape color
#> 1 1 <NA> 1 square red
#> 2 2 <NA> 2 hexagon green
#> 3 3 <NA> 3 triangle green
#> 4 4 <NA> 4 triangle green
#> 5 5 <NA> 5 square red