igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[igraph] How to rename vertices with contract.vertices() according to na


From: capitano . nemo
Subject: [igraph] How to rename vertices with contract.vertices() according to names of mapping vector
Date: Sun, 24 Nov 2013 05:08:11 +0000
User-agent: autistici.org webmail

Hello list,

Please consider the following graph with vertices of two categories ("digit" and "char"):

    id <- c("1","2","A","B")
    category <- c("digit","digit","char","char")
    from <- c("1","1","2","A","A","B")
    to <- c("2","A","A","B","1","2")

    nodes <- cbind(id,category)
    edges <- cbind(from,to)

    g <- graph.data.frame(edges, directed=TRUE, vertices=nodes)

Now I want to contract the vertices based on the attribute "category":

    category_attr <- get.vertex.attribute(g, "category")
    map = setNames(c(1:2), c("digit","char"))
    category_attr <- map[unlist(category_attr)]
    igraph_category <- contract.vertices(g, category_attr)
    print(get.data.frame(igraph_category, what="vertices"))
    #             name
    # c("1", "2") 1, 2
    # c("A", "B") A, B

Yet I would like the "id" and attribute "name" of the new graph to be the "category" of the contracted vertices, then I would like:

    print(get.data.frame(igraph_category, what="vertices"))
    #        name
    # digit    digit
    # char     char

How could I do it?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]