igraph-help
[Top][All Lists]

## Re: [igraph] degree according to vertex attributes

 From: Tamás Nepusz Subject: Re: [igraph] degree according to vertex attributes Date: Fri, 28 Sep 2012 23:44:19 +0200

```Hello,

Suppose that the city is encoded in the "city" vertex attribute of the graph;
for instance:

> g <- grg.game(100, 0.2)
> V(g)\$city <- LETTERS[(0:99 %/% 10) + 1]

First, we get the edge list of the graph:

> el <- get.edgelist(g)

Next, we select those edges (i.e. rows) from the edge list where the endpoints
are in the same city:

> internal.edges <- which(V(g)\$city[el[,1]] == V(g)\$city[el[,2]])

We then create a subgraph which consists of these edges only:

> internal.graph <- subgraph.edges(g, internal.edges)

degree(internal.graph) will then give you how many friends each person has in
the same city, and degree(g)-degree(internal.graph) will give you how many
friends each person has in different cities.

Best,
--
T.

On Friday, 28 September 2012 at 15:21, Dominik Santner (CRIE) wrote:

> Hello,
>
> I'm using igraph with R and I have an undirected graph including vertex
> attributes (for example the city of residence). I want to calculate the
> degree separately for connections to actors living in the same city and
> those living in any other city.
>
> Example: John lives in Berlin and has 10 friends (degree of 10). 3 of
> his friends live in Berlin, 3 in Hamburg, 2 in New York and 2 in Munich.
> I need a result telling me that John has 3 friends in the same city and
> 7 in any other city.
>
> Thanks
>
> Dominik
>
> _______________________________________________
> igraph-help mailing list