I have a matrix from which I construct a network graph with igraph. I want to store the information of different types of vertices as an attribute, which I can deduce from its names. I tried a couple of combinations
mymat = matrix(c(1,0),6,5)
colnames(mymat) <- c("tim", "tom","jane","tarzan", "maria")
rownames(mymat) <- c("tim", "tom","jane","tarzan", "maria", "megan")
M <- graph_from_incidence_matrix(mymat)
V(M)$gender <- ifelse(V(M) == startsWith(as.character(V(M)),"t"), "male","female")
V(M)$gender
[1] "female" "female" "female" "female" "female" "female" "female" "female" "female" "female" "female"
I started but forgot to call for the name of the attribute.
V(M)
V(M)$name
V(M)$gender <- ifelse(V(M)$name == startsWith(as.character(V(M)$name),"t"), "male","female")
Nothing there, too. By accident I called this:
V(M)$gender <- ifelse(V(M) == startsWith(as.character(V(M)$name),"t"), "male","female")
V(M)$gender
[1] "male" "female" "female" "female" "female" "female" "female" "female" "female" "female" "female"
But why not the others, I thought? Seems as it looks only at the first one?
V(M)$gender <- ifelse(V(M) == startsWith(as.character(V(M)$name),"to"), "male","female")
V(M)$gender
Again, nothing.
Is something wrong with how I use ifelse? Do I need to write a function?
Is there something wrong with how I call on the attributes' name?
off-topic: Why does graph_from_incidence_matrix or igraph in general create a "type"-attribute and on which grounds is "false" and "true" assigned here. You may see it here by
get.vertex.attribute(M)