Ben Nutzer
Ben Nutzer

Reputation: 1163

Transition edge attributes from bipartite to one mode graphs

I'd like to adopt an edge attribute from a bipartite graph for the one mode graph that is the result of command bipartite_projection.

library(igraph)

data <- data.frame( people= c(letters[1:5],letters[5:8],"a"),
             events=c(1,1,1,1,1,2,2,2,2,2),
             year=c(2004,2004,2004,2004,2004,2005,2005,2005,2005,2005))

g <- graph_from_data_frame(data)
V(g)$type <-  V(g)$name %in% data[,1] # Nodes are the people 
E(g)$year                             # this is what I want
proj <- bipartite.projection(g)

g2 <- proj[[2]]

Problems start here

E(g2)$year                       # here it is gone

el <- get.edgelist(g2)
el <- cbind(el, E(g)$year)    # ideally add it here to the edgelist

Does anyone know how to keep this edge attribute or how to index it properly, so that one can access it later on? Thanks in advance!

Edit for clarification (thanks @Heikki and @dllhell for your edit suggestion):

The idea behind this is to get the edgelist from a temporal network. The nodes are the people and the edges are the events. The events take place in certain years. To know which people met at the same event in the same year, I use the bipartite_projection.

Maybe this awfully edited picture helps:

enter image description here

So as you can see the edges are basically the events but unfortunately the information about the year goes missing. Maybe this clears things up.

Thanks for your help so far :-)

Upvotes: 1

Views: 698

Answers (1)

Barbara
Barbara

Reputation: 1168

Possible solution, try and let me know if it works:

library(igraph)

data <- data.frame( people= c(letters[1:5],letters[5:8],"a"),
                    events=c(1,1,1,1,1,2,2,2,2,2),
                    year=c(2004,2004,2004,2004,2004,2005,2005,2005,2005,2005))

g <- graph_from_data_frame(data)
V(g)$type <-  V(g)$name %in% data[,1] # Nodes are the people 
data$year_2 <- E(g)$year                             # this is what I want
proj <- bipartite.projection(g)

g2 <- proj[[2]]

E(g2)$year                       # here it is gone

el <- get.edgelist(g2)

el <- as.data.frame(el)

colnames(el)[1] <- "people"
el <- merge(el,data$year_2)

EDIT

Explanation The solution that I have found is to:

  1. Convert el into a dataframe
  2. Create new data frames (event1 and event2) for each of the events
  3. Create a new variable (year) in the el dataframe and use ifelse statements to assign the right value to that variable.

This is specific to a two events scenario but it can be easily extended. If you don't know the number of events you will get, you can just use a loop.

Code

library(igraph)

data <- data.frame( people= c(letters[1:5],letters[5:8],"a"),
                    events=c(1,1,1,1,1,2,2,2,2,2),
                    year=c(2004,2004,2004,2004,2004,2005,2005,2005,2005,2005))

g <- graph_from_data_frame(data)
V(g)$type <-  V(g)$name %in% data[,1] # Nodes are the people 
 E(g)$year                             # this is what I want
proj <- bipartite.projection(g)

g2 <- proj[[2]]

E(g2)$year                       # here it is gone

el <- get.edgelist(g2)

el <- as.data.frame(el)


event1 <- subset(data, events == 1)
event2 <- subset(data, events == 2)
el$year <- 0
el$year <- ifelse(el$V1 %in% event1$people & el$V2 %in% event1$people,  unique(event1$year), el$year )
el$year <-ifelse(el$V1 %in% event2$people & el$V2 %in% event2$people,unique(event2$year), el$year)

Upvotes: 1

Related Questions