how can I access (additional) vertex attributes after clustering?

Using igraph in R, I’m quoting from the manual v1.5.0, page35:

The first column of vertices is assumed to contain symbolic vertex names, this will be added to the graphs as the ‘name’ vertex attribute. Other columns will be added as additional vertex attributes.

I’m reproducing the example from manual page 36:

actors <- data.frame(
      name = c(
        "Alice", "Bob", "Cecil", "David",
        "Esmeralda"
      ),
      age = c(48, 33, 45, 34, 21),
      gender = c("F", "M", "F", "M", "F")
    )
    relations <- data.frame(
      from = c(
        "Bob", "Cecil", "Cecil", "David",
        "David", "Esmeralda"
      ),
      to = c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
      same.dept = c(FALSE, FALSE, TRUE, FALSE, FALSE, TRUE),
      friendship = c(4, 5, 5, 2, 1, 1), advice = c(4, 5, 5, 4, 2, 3)
    )
    g <- graph_from_data_frame(relations, directed = TRUE, vertices = actors)

All these vertices have attribute names like: name, age, gender. The others are edge attributes. For some reason, I cannot retrieve the attribute names using the following function:

graph_attr_names(g)
# character(0)

But this function gives you the vertex attributes from the 2nd dataframe ‘actors’.

vertex_attr_names(g)
# [1] "name"   "age"    "gender"

In the graph object I can access vertex attributes:

V(g)$name
# [1] "Alice"     "Bob"       "Cecil"     "David"     "Esmeralda"

But after clustering:

cl <- cluster_infomap(g)

the attributes seem to disappear, except for name which is renamed to names. So this works:

cl$names
# [1] "Alice"     "Bob"       "Cecil"     "David"     "Esmeralda"

But I cannot access age.

cl$age
# NULL

A function call returns the vertex names for all communities but not the other attributes.

communities(cl)
# $`1`
# [1] "Alice"     "Bob"       "Cecil"     "David"     "Esmeralda"

How can I access the additional vertex attributes mentioned in the manual? I’d like to see the age and gender for every community. Thank you very much for any information on this!

The attributes are stored in the graph, not the communities object. The communities object only contains information about the partitioning of vertices into groups. Extract the information from the graph for each group. I hope the following example helps:

> set.seed(42)
> g <- sample_gnm(10,20)
> V(g)$age <- runif(vcount(g), 20, 80)
> cl <- cluster_walktrap(g)
> groups(cl)
$`1`
[1] 2 3 4 5 6 7 8

$`2`
[1]  9 10

$`3`
[1] 1

> lapply(groups(cl), function (vs) V(g)[vs]$age)
$`1`
[1] 79.33350 76.80009 24.94625 50.85271 43.41221 74.34429 46.81818

$`2`
[1] 70.16026 64.25574

$`3`
[1] 28.32261

> membership(cl)
 [1] 3 1 1 1 1 1 1 1 2 2
> V(g)$age[ which(membership(cl) == 2) ]
[1] 70.16026 64.25574
2 Likes