[英]R Network Package - node attributes
I've recently started to learn R, with specific interest in network modeling applications. 我最近开始学习R,对网络建模应用程序特别感兴趣。 I've made a sample dataset and would like to visualize it, eventually getting to rigorous statistical network analysis.
我制作了一个样本数据集,并希望对其进行可视化,最终进行严格的统计网络分析。
The example is a high school friendship network. 例子是一个高中的友谊网络。 The node attributes are found in HS1_Node_Attributes.csv and the adjacency matrix is found in HS1_adjacency_matrix.
节点属性位于HS1_Node_Attributes.csv中,而邻接矩阵位于HS1_adjacency_matrix中。 I'm able to visualize the network, though I'm having trouble with node attributes (characteristics of the people).
尽管遇到节点属性(人员特征)方面的麻烦,但我可以可视化网络。 I'm using the network package.
我正在使用网络软件包。
The error I get is as follows: 我得到的错误如下:
Error in set.vertex.attribute(g, vertex.attrnames[[i]], vertex.attr[[i]]) : Inappropriate value given in set.vertex.attribute. set.vertex.attribute中的错误(g,vertex.attrnames [[i]],vertex.attr [[i]]):set.vertex.attribute中给出的值不合适。
I have cross-referenced my example with some tutorials online, along with the R Network package documentation. 我已经通过一些在线教程和R Network软件包文档交叉引用了我的示例。 The potential problem could have been the type of my attribute data frame, though I confirmed it was type list, which checks out.
潜在的问题可能是我的属性数据框的类型,尽管我确认它是类型列表,可以进行检出。 So I'm not sure what the problem is.
所以我不确定是什么问题。 Everything works fine (meaning I can successfully create a network object) if I don't take out node attributes (the
vertex.attr
and the vertex.attrnames
arguments), showing me that the rest of the code is sound. 如果我不删除节点属性(
vertex.attr
和vertex.attrnames
参数),一切都将正常工作(这意味着我可以成功创建网络对象),这向我表明其余的代码是正确的。 My code is below. 我的代码如下。
high_school1_attributes <- read.table("HS1_Node_Attributes.csv", header = TRUE,
sep = ",")
high_school1_adj <- read.table("HS1_adjacency_matrix.csv", header = TRUE,
row.names = 1, sep = ",")
adj1 <- as.matrix(high_school1_adj)
library("network")
high_school1_network <- network(adj1, vertex.attr = high_school1_attributes,
vertex.attrnames = colnames(high_school1_attributes),
directed = FALSE, hyper = FALSE, loops = FALSE,
multiple = FALSE, bipartite = FALSE)
You could be automatically converting strings to factors. 您可能会自动将字符串转换为因数。 I can recreate the error by doing:
我可以通过执行以下操作来重新创建错误:
high_school1_attributes <- read.csv(text=
"Name,Color
Kermit,green
Piggy,pink
Gonzo,blue")
high_school1_adj <- read.csv(text=
",From,To
1,1,3
2,3,2
3,2,1",
row.names = 1)
adj1 <- as.matrix(high_school1_adj)
library("network")
high_school1_network <- network(
adj1,
vertex.attr = high_school1_attributes,
vertex.attrnames = colnames(high_school1_attributes),
directed = FALSE, hyper = FALSE, loops = FALSE,
multiple = FALSE, bipartite = FALSE)
And can fix it by replacing the first statement with: 并可以通过将第一条语句替换为:
high_school1_attributes <- read.csv(text=
"Name,Color
Kermit,green
Piggy,pink
Gonzo,blue",
stringsAsFactors=FALSE)
Which you can see works by plotting: 您可以通过绘制以下内容来查看其作品:
library(igraph)
library(intergraph)
hs_graph <- asIgraph(high_school1_network)
plot(hs_graph, vertex.size=8,
vertex.color=V(hs_graph)$Color,
vertex.label=V(hs_graph)$Name,
edge.arrow.size=0.25,layout=layout.fruchterman.reingold)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.