简体   繁体   English

在 r 中使用网络 package 中的属性从节点列表创建没有边的图

[英]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, classunit应该是节点属性。 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.

相关问题 R / Network Analysis-如何通过节点的属性创建边 - R/Network Analysis - How to create edges by node's attributes R网络软件包-节点属性 - R Network Package - node attributes 使用R的网络图:基于连接边数的节点大小 - Network graph using R: Node size based on number of connected edges 如何用R(通过iGraph,network或其他软件包)在R的边缘两边绘制文本的有向网络图? - How to draw a directed network graph with texts on both sides of edges in R (via iGraph, network or some other package)? R网络软件包-有没有更快的方法来添加边? - R network package - is there a faster way to add edges? 设置特定的节点和边以创建图形网络 - Setting specific nodes and edges to create a graph network 在 R 中创建一个具有集合节点位置和集中边的网络图,其中包含圆头和箭头 - Creating a network graph with set node positions and concentrated edges with both circleheads and arrowheads in R 从R中的普通数据帧创建权重节点和边列表? - Create weight node and edges lists from a normal dataframe in R? R&Gephi:使用rgexf软件包无法正确导入网络中的边 - R & Gephi: edges in a network are not properly imported using the rgexf package 如何在R(igraph包)中显示图形的加权边之和? - How to display the sum of weighted edges of a graph in R (igraph package)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM