R:创建具有节点属性的"statnet"网络 [英] R: creating a 'statnet' network with node attributes

查看:526
本文介绍了R:创建具有节点属性的"statnet"网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此遵循有关使用"statnet"的示例. http://personal.psu.edu/drh20/papers/v24i09.pdf .

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

第一个示例显示了如何在R中检查statnet网络对象:

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)

在上面的示例中,这里的statnet网络似乎已经具有节点属性".

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

使用statnet库,有谁知道是否有一种方法可以直接从数据帧中创建具有节点属性的网络?

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")

)

我尝试了以下代码:

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?

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

谢谢

推荐答案

Igraph可以,但是如果要保留在statnet套件即网络包中,则可以执行以下操作:

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")

set.vertex.attribute函数似乎不接受因素,因此as.character()

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

如果您有很多顶点属性,则可以使用"apply"(应用)将set.vertex.attribute作为函数应用到具有顶点属性的数据框的列上.

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.

通常,Michael Heaney的statnet套件的材料(教程)对我来说非常有用(第一个项目符号中链接的标题为"Summer Workshops"的材料):

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

这篇关于R:创建具有节点属性的"statnet"网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆