Add one or more vertex attributes to a graph

add_vertex_attributes(x, attr_name = NULL, value)

Arguments

x

graph object to which the vertex attributes need to be added

attr_name

optional, character vector with attribute names (see 'Details')

value

data.frame, matrix (with column names), or a vector (see 'Details')

Value

graph object with new attributes added

Details

Consistent API to easily add one or more vertex attributes to a graph of class igraph or network. Both the igraph and statnet packages do not have (simple) functions for this.

Adding vertex attributes is easy, There are several options:

  • a vector with values specified in value and a name for the attribute specified in attr_name.

  • a data.frame for the value. All columns in the data.frame will be added to the graph as vertex attributes and the names in the data.frame are used as the names of the new attributes,

  • a matrix for the value. The matrix needs to have appropriate column names. All columns in the matrix will be added to the graph as vertex attributes and the column names in the matrix are used as the names of the new attributes,

  • a data.frame or matrix (with column names) AND a character vector with variable names in attr_name. The variables with the names specified in the attr_name argument will be taken from the data.frame or matrix and will be added to the graph as vertex attributes. This is useful in case you have a larger data.frame of which you only want to add some of the columns as vertex attributes.

In all cases, the attributes will be added for all vertices, so you need to make sure the values in the correct order and exist for all of the vertices.

Examples

g <- igraph::graph.ring(5)
#> Registered S3 methods overwritten by 'igraph':
#>   method       from  
#>   plot.igraph  snafun
#>   print.igraph snafun
add_vertex_attributes(g, "at1", 1:5) |> 
  igraph::vertex_attr("at1")
#> [1] 1 2 3 4 5

add_vertex_attributes(g, value = data.frame(naam = 1:5)) |> 
  igraph::vertex_attr("naam")
#> [1] 1 2 3 4 5

add_vertex_attributes(g, value = data.frame(een = 11:15, twee = 21:25))
#> IGRAPH c433991 U--- 5 5 -- Ring graph
#> + attr: name (g/c), mutual (g/l), circular (g/l), een (v/n), twee (v/n)
#> + edges from c433991:
#> [1] 1--2 2--3 3--4 4--5 1--5

add_vertex_attributes(g, c("twee", "een"), 
  value = data.frame(een = 11:15, twee = 21:25, drie = 31:35))
#> IGRAPH c433991 U--- 5 5 -- Ring graph
#> + attr: name (g/c), mutual (g/l), circular (g/l), twee (v/n), een (v/n)
#> + edges from c433991:
#> [1] 1--2 2--3 3--4 4--5 1--5

g <- sna::rgraph(10, tprob = .2) |> network::as.network()
#> Registered S3 methods overwritten by 'network':
#>   method        from  
#>   plot.network  snafun
#>   print.network snafun
add_vertex_attributes(g, "naam", value = data.frame(naam = 1:10))
#>  Network attributes:
#>   vertices = 10 
#>   directed = TRUE 
#>   hyper = FALSE 
#>   loops = FALSE 
#>   multiple = FALSE 
#>   bipartite = FALSE 
#>   total edges= 17 
#>     missing edges= 0 
#>     non-missing edges= 17 
#> 
#>  Vertex attribute names: 
#>     naam vertex.names 
#> 
#> No edge attributes

add_vertex_attributes(g, value = data.frame(naam = 1:10))
#>  Network attributes:
#>   vertices = 10 
#>   directed = TRUE 
#>   hyper = FALSE 
#>   loops = FALSE 
#>   multiple = FALSE 
#>   bipartite = FALSE 
#>   total edges= 17 
#>     missing edges= 0 
#>     non-missing edges= 17 
#> 
#>  Vertex attribute names: 
#>     naam vertex.names 
#> 
#> No edge attributes

add_vertex_attributes(g, value = data.frame(een = 10:19, twee = 20:29))
#>  Network attributes:
#>   vertices = 10 
#>   directed = TRUE 
#>   hyper = FALSE 
#>   loops = FALSE 
#>   multiple = FALSE 
#>   bipartite = FALSE 
#>   total edges= 17 
#>     missing edges= 0 
#>     non-missing edges= 17 
#> 
#>  Vertex attribute names: 
#>     een twee vertex.names 
#> 
#> No edge attributes

add_vertex_attributes(g, c("twee", "een"), 
  value = data.frame(een = 10:19, twee = 20:29, drie = 30:39))
#>  Network attributes:
#>   vertices = 10 
#>   directed = TRUE 
#>   hyper = FALSE 
#>   loops = FALSE 
#>   multiple = FALSE 
#>   bipartite = FALSE 
#>   total edges= 17 
#>     missing edges= 0 
#>     non-missing edges= 17 
#> 
#>  Vertex attribute names: 
#>     een twee vertex.names 
#> 
#> No edge attributes