Assign value to all upstream nodes in R igraph based on downstream value

Viewed 33

I'm out of my depth when it comes to network graphs, but I have a table of ~6300 From/To links similar to the data frame df given below. Each vertex has a binary property called status.

What I would like to do is determine all of the vertices that are upstream of a vertex where status = 1, how would i do this in igraph? I've looked at data.tree but my data are not necessarily a single-root tree and "loops" are possible.

In the example below, this would mean that vertices Z, R, S, M, and K should have status = 1 (i.e. be orange in the plot), as they are upstream of Q, L, I, respectively.

library(igraph)

df <- data.frame(from = c("D","B","A","Q","Z","L","M","R","S","T","U","H","I","K"),
                 to = c("O","D","B","B","Q","O","L","Q","R","O","T","J","J","I"),
                 stringsAsFactors = FALSE
                 )

vertices <- data.frame(vertex = unique(c(df[,1], c(df[,2]))),
                       status = c(0,0,0,1,0,1,0,0,0,0,0,1,
                                  1,0,0,0))

g <- graph_from_data_frame(df, vertices = vertices, directed = T)
plot(g, vertex.color = vertex_attr(g, "status"))

network sample

1 Answers

You can use subcomponent with mode='in'.

Related