简体   繁体   中英

How can node have list type attributes using igraph in R?

Actually, I want to set each node's specific attributes to list and am trying this pattern:

1) read edge list

net <- read.graph(path)

2) set all the node's community information as empty list

net <- set.vertex.attribute(net, "community", V(net), value=list())

3) Test

net <- set.vertex.attribute(net, "community", V(net)[1], c(get.vertex.attribute(net, "community", V(net)[1]),4244))

get.vertex.attribute(net, "community", V(net)[1])

Test result is good: they right value is returned.

But, I want to concatenate the each node's community value — like, V(net)[1] 's value is c(1, 3, 4, 5, …) — but am not sure how to proceed.

This will set the node attributes to a list:

library(igraph)
g <- graph.famous("Bull")           # sample
# to set attribute of one vertex
V(g)[1]$community <- list(c(1,1,1))
V(g)[1]$community[[1]]
# [1] 1 1 1

# to set attributes of all vertices
V(g)$community <- sapply(1:vcount(g),function(i)list(sample(1:5,5)))
V(g)$community
# [[1]]
# [1] 1 2 4 5 3
# 
# [[2]]
# [1] 2 3 5 1 4
# 
# [[3]]
# [1] 3 2 5 1 4
# 
# [[4]]
# [1] 3 1 5 2 4
# 
# [[5]]
# [1] 1 5 2 4 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM