0

Is it possible to construct a graph in network that only contains nodes but not edges from a data frame? 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. I could also work with an igraph solution but I'm curious if this is possible.

4

1 回答 1

0

我想我找到了使用 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)

于 2020-04-28T13:12:28.633 回答