0

I have a bipartite graph and a data frame with each rows associated with each vertices of the first side of the graph. The graph is not connected and when I find the largest component of the graph, I have to subset the data frame (which does not give me the correct answer). Another possible option is to set the the rows of the data frame as the attribute of the vertices of the first side of the graph (which I don't know how to do it)!. Here is a toy example:

edgelist = matrix(c("A","a","A","b","B","b","C","c","D","c"),ncol=2,byrow=T)
bg <- graph.data.frame(edgelist, directed=F)
V(bg)$type <- V(bg)$name %in% edgelist[,1]
summary(bg)
V(bg)[V(bg)$type==1]
df = data.frame(id=c("A","B","C","D"), x=runif(4,10,50), y=sample(4), z=rnorm(4))

gclust = clusters(bg)
numClust = gclust$no 
numLCC = gclust$csize[1] 
bg2 = induced.subgraph(bg, which(gclust$membership ==which.max(gclust$csize)))
remained_set1 <- V(bg2)[V(bg2)$type==1]

df[as.character(df[,1])%in%remained_set1,] # wrong answer
mathlover
  • 71
  • 7

1 Answers1

0

Try using the names of the vertices and flipping the %in%

subset(df, id %in% names(remained_set1))

This returns the rows for A and B in your example.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • I get the same thing. i.e. [1] id x y z <0 rows> (or 0-length row.names) – mathlover Aug 28 '17 at 17:49
  • Really? You copied and pasted using the example data above? What version of igraph you are using? This was tested with `igraph_1.1.2`. – MrFlick Aug 28 '17 at 17:50
  • Yes, Mine is igraph_0.7.1 . I guess my igraph is messed up. I might need to update my R since when I install igraph, this version is installed. I asked this question yesterday https://stackoverflow.com/questions/45892699/r-igraph-graph-from-incidence-matrix-and-graph-from-adjacency-matrix-dont?noredirect=1#comment78760630_45892699 – mathlover Aug 28 '17 at 17:59
  • I updated R and reinstalled igraph and it worked. Thanks. (If you know how to do the other option, i.e. adding rows of the data frame as vertex attribute, I really appreciate if you answer that as well) – mathlover Aug 28 '17 at 18:10