Add vertex names to a graph

add_vertex_names(x, value, vids = NULL)

Arguments

x

graph object to which the vertex attributes need to be added

value

the vertex names to be set

vids

the id's of the vertices for which the names are to be set. This is useful if you only want to set/correct the names of a few vertices. For igraph objects, these can be the number or the name of the vertices. For network objects, only the numeric ID is allowed. The default is NULL, which selects all vertices, in the order in which they occur in the graph object.

Value

graph object with new attributes added

Details

Consistent API to easily add vertex names to a graph of class igraph or network. The vertex attribute that holds the vertex names is called differently for igraph and network objects, so if you use the incorrect name for the attribute, the new names will not be recognized in subsequent steps of the network analysis pipeline.

This function puts the vertex names in the correct vertex attribute, so you do not need to remember what they are called.

Examples

g <- snafun::create_random_graph(5, "gnp", p = .1, graph = "igraph")
# there are no names yet
snafun::extract_vertex_names(g)  # NULL
#> NULL
g <- snafun::add_vertex_names(g, LETTERS[5:1])
snafun::extract_vertex_names(g) # "E" "D" "C" "B" "A"
#> [1] "E" "D" "C" "B" "A"
g <- snafun::add_vertex_names(g, "X", vids = 2)
snafun::extract_vertex_names(g) # "E" "X" "C" "B" "A"
#> [1] "E" "X" "C" "B" "A"
g <- snafun::add_vertex_names(g, c("Y", "Z"), vids = c("C", "A"))
snafun::extract_vertex_names(g) # ""E" "X" "Y" "B" "Z"
#> [1] "E" "X" "Y" "B" "Z"

g <- snafun::create_random_graph(5, "gnp", p = .1, graph = "network")
snafun::extract_vertex_names(g)  # 1 2 3 4 5
#> [1] 1 2 3 4 5
g <- snafun::add_vertex_names(g, LETTERS[5:1])
snafun::extract_vertex_names(g) # "E" "D" "C" "B" "A"
#> [1] "E" "D" "C" "B" "A"
g <- snafun::add_vertex_names(g, "X", vids = 2)
snafun::extract_vertex_names(g) # "E" "X" "C" "B" "A"
#> [1] "E" "X" "C" "B" "A"
# network objects accept only numeric id's
g <- snafun::add_vertex_names(g, c("Y", "Z"), vids = c(3, 5))
snafun::extract_vertex_names(g) # ""E" "X" "Y" "B" "Z"
#> [1] "E" "X" "Y" "B" "Z"