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

**URL:** <https://igraph.discourse.group/t/how-can-i-access-additional-vertex-attributes-after-clustering/1688>\
**Category:** Usage\
**Tags:** R\
**Created:** [21 October 2023 08:33 UTC](https://igraph.discourse.group/t/how-can-i-access-additional-vertex-attributes-after-clustering/1688 "2023-10-21T08:33:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![astruck](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@astruck](https://igraph.discourse.group/u/astruck)\
**Post date:** [21 October 2023 08:33 UTC](https://igraph.discourse.group/t/how-can-i-access-additional-vertex-attributes-after-clustering/1688/1 "2023-10-21T08:33:52Z")

</div>

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:

```auto
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:

```auto
graph_attr_names(g)
# character(0)

```

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

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

```

In the graph object I can access vertex attributes:

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

```

But after clustering:

```auto
cl <- cluster_infomap(g)

```

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

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

```

But I cannot access `age`.

```auto
cl$age
# NULL

```

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

```auto
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!

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [21 October 2023 11:21 UTC](https://igraph.discourse.group/t/how-can-i-access-additional-vertex-attributes-after-clustering/1688/2 "2023-10-21T11:21:17Z")

</div>

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:

```auto
> 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

```
