[英]Create graph with no edges from node list with attributes in the network package in r
Is it possible to construct a graph in network
that only contains nodes but not edges from a data frame?是否可以在network
中构建一个只包含节点但不包含数据帧边的图? The data structure looks as follows:数据结构如下所示:
library(statnet)
ID <- as.character(rep(1:10, each = 1, times = 1))
class <- rep(c("class1","class2"), each = 5, times = 1)
unit <- rep(c('unit1', 'unit2'), each = 1, times = 5)
net_data <- as.data.frame(cbind(ID, class, unit)))
ID
is the node ID and class
and unit
are supposed to be node attributes. ID
是节点 ID, class
和unit
应该是节点属性。 I could also work with an igraph
solution but I'm curious if this is possible.我也可以使用igraph
解决方案,但我很好奇这是否可能。
I think I found a workaround using igraph - might be useful for other people so I will post my solution.我想我找到了使用 igraph 的解决方法 - 可能对其他人有用,所以我将发布我的解决方案。
library(statnet)
library(igraph)
library(intergraph)
class <- rep(c("class1","class2"), each = 5, times = 1)
unit <- rep(c('unit1', 'unit2'), each = 1, times = 5)
#create empty graph with igraph
g <- make_empty_graph(n = 10, directed = FALSE)
att_data_frame <- as.data.frame(cbind(class, unit))
att_data_frame$class <- as.character(att_data_frame$class)
att_data_frame$unit <- as.character(att_data_frame$unit)
for(cn in colnames(att_data_frame)) {
g = set_vertex_attr(g, cn, 1:nrow(att_data_frame), value=att_data_frame[,cn])
}
g <- asNetwork(g)
class(g)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.