How to get vertex ids back from graph

前端 未结 2 1756
死守一世寂寞
死守一世寂寞 2021-02-04 06:05

Please consider the following

library(igraph)
id <- c(\"1\",\"2\",\"A\",\"B\")
name <- c(\"02 653245\",\"03 4542342\",\"Peter\",\"Mary\")
category <- c(         


        
2条回答
  •  情书的邮戳
    2021-02-04 06:45

    The answer might be useful. My recommendation is same as of @Gabor Csardi about id as name, and name as real_name.

    library(igraph)
    name <- c("1","2","A","B")
    real_name <- c("02 653245","03 4542342","Peter","Mary")
    category <- c("digit","digit","char","char")
    from <- c("1","1","2","A","A","B")
    to <- c("2","A","A","B","1","2")
    
    nodes <- cbind(name,real_name,category)
    edges <- cbind(from,to)
    
    g <- graph.data.frame(edges, directed=TRUE, vertices=nodes)
    
    list.vertex.attributes(g)
    #Output: [1] "name"      "real_name" "category"
    which(V(g)$real_name == "Peter")
    #Output: [1] 3
    V(g)$name[which(V(g)$real_name == "Peter")]
    #Output: [1] "A"
    

提交回复
热议问题