attributes {igraph}R Documentation

Graph, vertex and edge attributes

Description

Attributes are associated values belonging to a graph, vertices or edges. These can represent some property, like data about how the graph was constructed, the color of the vertices when the graph is plotted, or simply the weights of the edges in a weighted graph.

Usage

graph <- add.graph.attribute(graph, attrname)
graph <- remove.graph.attribute(graph, attrname)
get.graph.attribute(graph, attrname=NULL)
set.graph.attribute(graph, attrname, value)
g.a(graph, attrname=NULL)
g.a(graph, attrname) <- value
graph <- add.vertex.attribute(graph, attrname)
graph <- remove.vertex.attribute(graph, attrname)
get.vertex.attribute(graph, attrname=NULL, v=igraph.vs.all(graph))
set.vertex.attribute(graph, attrname, v=igraph.vs.all(graph), value)
v.a(graph, attrname=NULL, v=igraph.vs.all(graph))
v.a(graph, attrname, v=igraph.vs.all(graph)) <- value
graph <- add.edge.attribute(graph, attrname)
graph <- remove.edge.attribute(graph, attrname)
get.edge.attribute(graph, attrname=NULL, e=igraph.es.all(graph))
set.edge.attribute(graph, attrname, e=igraph.es.all(graph), value)
e.a(graph, attrname=NULL, e=igraph.es.all(graph))
e.a(graph, attrname, e=igraph.es.all(graph)) <- value

Arguments

graph The graph object to work on. Note that the original graph is never modified, a new graph object is returned instead; if don't assign it to a variable your modifications will be lost! See examples below.
attrname Character constant, the name of the attribute. For query functions this can be NULL, in this case the list of the attributes will be returned.
v Numeric vector, the ids of the vertices. It is not recycled, even if value is longer.
value Numeric vector, the new value(s) of the attributes, it will be recycled if needed.
e Numeric vector, the ids of the edges. It is not recycled, even if value is longer.

Details

An attribute value is either a numeric constant or a character string.

Each attribute has a name, this is simply a character string. The names of the graph, vertex and edge attributes use different name spaces, so the ‘color’ vertex attribute has nothing to do with the ‘color’ edge attribute.

In order to use an attribute it has to be ‘added’ first by using the add.*.attribute functions. The initial values of the attributes are undefined. After an attribute is added, its value(s) can be set by the set.*.attribute functions and queried by the get.*.attribute functions.

You can also use the shorter g.a, v.a, e.a forms, see the examples below, they are shorthands for the get.*.attribute and set.*.attribute functions, the latter is in effect if it is used on the left hand side of an assignment, see examples below.

An unneeded attribute can be removed by the remove.*.attribute functions.

You can list the attributes of a graph by giving attrname=NULL to the query functions (this is the default), or by print.igraph.

Value

The add.*.attribute and remove.*.attribute and set.*.attribute functions all return a new graph object with the requested modifications.
The get.*.attribute functions return numeric or character vectors, the values of the attributes if the attrname argument is not NULL and a character vector, the name of the attributes if it is NULL.

Note

Only numeric constant attributes are implemented right now, string attributes will be added shortly.

Author(s)

Gabor Csardi csardi@rmki.kfki.hu

See Also

print.igraph can also print attributes

Examples

g <- graph.star(10)
g <- add.graph.attribute(g, "id")
g.a(g, "id")
remove.graph.attribute(g, "id") ## g is unchanged
g.a(g)
g.a(g, "id")

g <- add.vertex.attribute(g, "size")
v.a(g, "size") <- c(1,2)
v.a(g, "size")

g <- add.edge.attribute(g, "weight")
e.a(g, "weight") <- sample(1:100, 20, replace=TRUE)
e.a(g, "weight")

print.igraph(g, g=TRUE, v=TRUE, e=TRUE)

g2 <- graph.star(10)
g2 <- add.graph.attribute(g2, "date", "character")
g.a(g2, "date")
g.a(g2, "date") <- "2005/10/25"
g.a(g2, "date")
remove.graph.attribute(g2, "date") ## g2 is unchanged
g.a(g2)
g.a(g2, "date")

g2 <- add.vertex.attribute(g2, "id", "character")
abc <- strsplit("abcdefghujklmnpqrstuvwxyz", "")[[1]]
v.a(g2, "id") <- abc
v.a(g2, "id")

g2 <- add.edge.attribute(g2, "random", "character")
e.a(g2, "random") <- sample(1:100, 20, replace=TRUE)
e.a(g2, "random")

print.igraph(g2, g=TRUE, v=TRUE, e=TRUE)

[Package igraph version 0.1.2 Index]