Retrieve the edge weights of a graph

A generic function that returns the edge weights of a graph. If index is specified, only the weights for the edges from the specified nodes are returned. The user can control which edge attribute is interpreted as the weight, see the Details section.

edgeWeights(object, index, ..., attr = "weight", default = 1, type.checker = is.numeric)
A graph, any object that inherits from the graph class.
If supplied, a character or numeric vector of node names or indices.
The name of the edge attribute to use as a weight. You can view the list of defined edge attributes and their default values using edgeDataDefaults. The default attribute name is "weight", see the Details section.
The value to use if object has no edge attribute named by the value of attr. The default is the value 1 (double).
A function that will be used to check that the edge weights are of the correct type. This function should return TRUE if the input vector is of the right type and FALSE otherwise. The default is to check for numeric edge weights using is.numeric. If no type checking is desired, specify NULL.

If index is suppled, then edge weights from these nodes to all adjacent nodes are returned. If index is not supplied, then the edge weights for all nodes are returned. The value for nodes without any outgoing edges will be a zero-length vector of the appropriate mode.

The edgeWeights method is a convenience wrapper around edgeData, the general-purpose way to access edge attribute information for a graph instance. In general, edge attributes can be arbitary R objects. However, for edgeWeights to make sense, the values must be vectors of length not more than one.

By default, edgeWeights looks for an edge attribute with name "weight" and, if found, uses these values to construct the edge weight list. You can make use of attributes stored under a different name by providing a value for the attr argument. For example, if object is a graph instance with an edge attribute named "WTS", then the call edgeWeights(object, attr="WTS") will attempt to use those values.

The function specified by type.checker will be given a vector of edge weights; if the return value is not TRUE, then an error will be signaled indicating that the edge weights in the graph are not of the expected type. Type checking is skipped if type.checker is NULL.

If the graph instance does not have an edge attribute with name given by the value of the attr argument, default will be used as the weight for all edges. Note that if there is an attribute named by attr, then its default value will be used for edges not specifically customized. See edgeData and edgeDataDefaults for more information.

Because of their position after the ..., no partial matching is performed for the arguments attr, default, and type.checker.


A named list of named edge weight vectors. The names on the list are the names of the nodes specified by index, or all nodes if index was not provided. The names on the weight vectors are node names to identify the edge to which the weight belongs.

See Also

nodes edges edgeData edgeDataDefaults is.numeric is.integer is.character

  • edgeWeights
  V <- LETTERS[1:4]
  edL2 <- vector("list", length=4)
  names(edL2) <- V
  for(i in 1:4)
    edL2[[i]] <- list(edges=c(2,1,2,1)[i], weights=sqrt(i))
  gR2 <- graphNEL(nodes=V, edgeL=edL2, edgemode="directed")
  edgeWeights(gR2, "C")
  edgeWeights(gR2, attr="foo", default=5)
  edgeData(gR2, attr="weight")
  edgeData(gR2, from="C", attr="weight")
Documentation reproduced from package graph, version 1.50.0, License: Artistic-2.0

Community examples

Looks like there are no examples yet.