[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[igraph] How to use contract.vertices() on empty string
From: |
capitano . nemo |
Subject: |
[igraph] How to use contract.vertices() on empty string |
Date: |
Sat, 23 Nov 2013 03:15:22 +0000 |
User-agent: |
autistici.org webmail |
I have a network I would like to contract over the attribute 'category'.
BUT some vertices take the value "" (empty string) for the attribute
category. This makes contract.vertices() gives the error:
Error in contract.vertices(pg$proj2, category_attr) :
At structure_generators.c:84 : Invalid (negative) vertex id, Invalid
vertex id
What's the best strategy to avoid this error? (Note that I can't replace
'category' with 'NA', which for the vertices type=FALSE takes another
meaning in my network.)
I was able to replicate my situation in a MWE
id <- c("www.user_a.com","www.user_b.com",
"www.user_c.com","www.user_d.com",
"www.user_e.com","www.thread_1.com",
"www.thread_2.com","www.thread_3.com",
"www.thread_4.com","www.thread_5.com")
type <- c(FALSE,FALSE,
FALSE,FALSE,
FALSE,TRUE,
TRUE,TRUE,
TRUE,TRUE)
name <- c("user_a","user_b",
"user_c","user_d",
"user_e","thread_1",
"thread_2","thread_3",
"thread_4","thread_5")
category <- c(NA,NA,
NA,NA,
NA,"Dev",
"Traffic","Energy",
"","Dev")
nodes <- data.frame(id,name,type,category)
from <- c("www.user_a.com","www.user_b.com",
"www.user_c.com","www.user_d.com",
"www.user_e.com","www.user_d.com",
"www.user_c.com","www.user_b.com",
"www.user_a.com","www.user_e.com",
"www.user_c.com","www.user_e.com")
to <-c("www.thread_1.com","www.thread_2.com",
"www.thread_3.com","www.thread_4.com",
"www.thread_5.com","www.thread_1.com",
"www.thread_2.com","www.thread_3.com",
"www.thread_4.com","www.thread_5.com",
"www.thread_5.com","www.thread_1.com")
edges <- data.frame(from,to)
g <- graph.data.frame(edges, directed=TRUE, vertices=nodes)
pg <- bipartite.projection(g)
category_attr <- get.vertex.attribute(pg$proj2, "category")
map = setNames(c(1:4), c("Dev","Traffic","Energy",""))
category_attr <- map[unlist(category_attr)]
igraph_category <- contract.vertices(pg$proj2, category_attr)
## Error in contract.vertices(pg$proj2, category_attr) :
## At structure_generators.c:84 : Invalid (negative) vertex id, Invalid
vertex id
- [igraph] How to use contract.vertices() on empty string,
capitano . nemo <=