R: creating a 'statnet' network with node attributes

限于喜欢 提交于 2020-11-29 11:12:00

问题


I am following the examples over here on using the "statnet" library in http://personal.psu.edu/drh20/papers/v24i09.pdf.

The first example shows how to inspect a statnet network object in R:

library(statnet)
library(network)
 data("faux.magnolia.high") 
 fmh <- faux.magnolia.high 
summary(fmh)

In the above example, it seems here that the statnet network in this example already has "node attributes".

Using the statnet library, does anyone know if there is a way to directly create a network with node attributes from a data frame?

For example, if I have some data that looks like this:

mydata <-data.frame(

"source" = c("123","124","123","125","123"),
"target" = c("126", "123", "125", "122", "111"),
"color" = c("red","red","green","blue","red"),
"food" = c("pizza","pizza","cake","pizza","cake")
)

Suppose I had a pre defined list of node attributes:

Nodes <-data.frame(

"source" = c("123","124","125","122","111", "126"),

"Country" = c("usa", "uk", "uk", "usa", "uk", "usa")

)

I tried the following code:

net = network(mydata)

But I am not sure if this has created a network with the node attributes (color and food).

I also tried this, but it did not work:

mydata <-data.frame(

"source" = c("123","124","123","125","123"), "target" = c("126", "123", "125", "122", "111"), "color" = c("red","red","green","blue","red"), "food" = c("pizza","pizza","cake","pizza","cake") )

Nodes <-data.frame(

"source" = c("123","124","125","122","111", "126"),

"Country" = c("usa", "uk", "uk", "usa", "uk", "usa")

)

net<-network(mydata[,c[1:2])

edges <- as.sociomatrix(mydata[,c(3:4)],simplify=TRUE)

nodes <- as.sociomatrix(Nodes,simplify=TRUE)

final <- as.sociomatrix(list(net,edges,nodes))

Can someone please show me how to create a network with node attributes?

source: https://rdrr.io/github/statnet/network/man/as.sociomatrix.html

Thanks


回答1:


Could this be the answer? https://igraph.org/r/doc/graph_from_data_frame.html

library(igraph)

g <- graph_from_data_frame(mydata, directed=TRUE, vertices=Nodes)



回答2:


Igraph is OK, but if you want to remain within the network package, that is statnet suite, you could do the following:

net<- as.network(mydata, matrix.type = "edgelist")
set.vertex.attribute(net, "color", as.character(mydata$color))
set.vertex.attribute(net, "food", as.character(mydata$food))
#To verify...
get.vertex.attribute(net, "color")

It seems that the set.vertex.attribute function does not accept factors, hence as.character()

If you have a lot of vertex attributes you could use "apply" to apply the set.vertex.attribute as a function over columns of a data frame with vertex attributes.

Generally, the materials (tutorials) for statnet suite from Michael Heaney were very useful to me (materials linked in the first bullet under the title "Summer Workshops"): http://michaeltheaney.com/teaching



来源:https://stackoverflow.com/questions/64852237/r-creating-a-statnet-network-with-node-attributes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!