由数据帧中的二进制和字符变量创建双模网络 [英] Creating two-mode network from binary and character variables in data frame
本文介绍了由数据帧中的二进制和字符变量创建双模网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
library(statnet)
org <- c("A","B","C","D","E","F","G","H","I","J")
link <- c(1,0,0,0,1,1,0,0,1,1)
person <- c("Mary","Michael","Mary","Jane","Jimmy",
"Johnny","Becky","Bobby","Becky","Becky")
df <- data.frame(org,link,person)
socmat1 <- tcrossprod(df$link)
rownames(socmat1) <- df$org
colnames(socmat1) <- df$org
diag(socmat1) <- 0
socmat1
#> A B C D E F G H I J
#> A 0 0 0 0 1 1 0 0 1 1
#> B 0 0 0 0 0 0 0 0 0 0
#> C 0 0 0 0 0 0 0 0 0 0
#> D 0 0 0 0 0 0 0 0 0 0
#> E 1 0 0 0 0 1 0 0 1 1
#> F 1 0 0 0 1 0 0 0 1 1
#> G 0 0 0 0 0 0 0 0 0 0
#> H 0 0 0 0 0 0 0 0 0 0
#> I 1 0 0 0 1 1 0 0 0 1
#> J 1 0 0 0 1 1 0 0 1 0
testnet <- as.network(x = socmat1,
directed = FALSE,
loops = FALSE,
matrix.type = "adjacency"
)
testnet
#> Network attributes:
#> vertices = 10
#> directed = FALSE
#> hyper = FALSE
#> loops = FALSE
#> multiple = FALSE
#> bipartite = FALSE
#> total edges= 10
#> missing edges= 0
#> non-missing edges= 10
#>
#> Vertex attribute names:
#> vertex.names
#>
#> No edge attributes
由reprex package(v0.3.0)在2020-10-24创建
但是,我显然不能以类似的方式使用tcrossprod()
来实现与组织连接的个人相同的结果,反之亦然,如以下代码所示:
socmat2 <- tcrossprod(df$org)
#> Error in df$org: object of type 'closure' is not subsettable
rownames(socmat2) <- df$person
#> Error in df$person: object of type 'closure' is not subsettable
colnames(socmat2) <- df$person
#> Error in df$person: object of type 'closure' is not subsettable
diag(socmat2) <- 0
#> Error in diag(socmat2) <- 0: object 'socmat2' not found
socmat2
#> Error in eval(expr, envir, enclos): object 'socmat2' not found
如何创建双模网络,第一组边是组织在较大组织中的成员身份(用link变量表示),第二组边是个人在组织中的领导职位?
谢谢大家。
由reprex package(v0.3.0)在2020-10-24创建
推荐答案
有许多不同的方法可以执行您尝试执行的操作。我不知道有什么功能可以根据您拥有的数据神奇地创建一个双模网络,所以下面的解决方案涉及一些数据操作。我们首先创建一个包含节点的数据框,然后创建另一个包含边的数据框。然后使用节点和边作为输入来创建network
对象。代码不言自明:
library(tidyverse)
library(network)
# Let's create a 'nodes' data frame
my_nodes <- as.data.frame(rbind(
cbind(nodename = org, type = "Organization"),
cbind(unique(person), "People"),
cbind("Parent", "Parent org")))
# Let's add an ID column to the nodes data frame
my_nodes <- rowid_to_column(my_nodes, "ID")
# Let's create a data frame with al possible edges
# (i.e., connecting organizations to people and organizations to the parent organization)
my_edges <- data.frame(rbind(
cbind(ColA = org, ColB = person, type = "Set 1"),
cbind(org, link, "Set 2")))
my_edges <- subset(my_edges, ColB != 0)
my_edges$ColB[my_edges$ColB == 1] <- "Parent"
# Let's set up the network object using edges and nodes
my_network <- network(my_edges,
vertex.attr = my_nodes,
matrix.type = "edgelist",
ignore.eval = FALSE)
请注意,我们创建了一列type
来对节点和边进行分类。我们可以在可视化网络时使用type
更改节点/边缘的颜色、大小、形状等。
这里是一个使用包igraph
的示例。首先,我们将network
对象转换为igraph
对象。
library(igraph)
library(intergraph)
my_netgraph <- asIgraph(my_network)
可以使用V(my_netgraph)$attribute_name
评估节点的属性。例如,让我们看看我们在前面定义的网络中的type
节点:
> V(my_netgraph)$type
[1] "Organization" "Organization" "Organization" "Organization" "Organization" "Organization"
[7] "Organization" "Organization" "Organization" "Organization" "People" "People"
[13] "People" "People" "People" "People" "People" "Parent org"
现在让我们根据type
为这些节点上色。为此,我们将创建一个新属性$color
。每个$color
应对应于不同的$type
:
V(my_netgraph)[V(my_netgraph)$type == "People"]$color <- "green"
V(my_netgraph)[V(my_netgraph)$type == "Organization"]$color <- "red"
V(my_netgraph)[V(my_netgraph)$type == "Parent org"]$color <- "yellow"
plot(my_netgraph)
现在的网络是这样的:
现在让我们根据属性$type
更改节点的$shape
:
V(my_netgraph)[V(my_netgraph)$type == "People"]$shape <- "circle"
V(my_netgraph)[V(my_netgraph)$type == "Organization"]$shape <- "square"
V(my_netgraph)[V(my_netgraph)$type == "Parent org"]$shape <- "rectangle"
plot(my_netgraph)
我们可以使用以下函数更改igraph
对象的其他属性:
E(my_netgraph) # changes he edges of the "net" object
V(my_netgraph) # changes the vertices of the "net" object
E(my_netgraph)$type # changes edge attribute "type"
V(my_netgraph)$media # changes the vertex attribute "media"
您可以在this iGraph manual(第10-11页)上找到更多详细信息。
这篇关于由数据帧中的二进制和字符变量创建双模网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文