[英]Creating two-mode network from binary and character variables in data frame
SNA 人员:我正在尝试从 R 中的数据框创建一个 two-mode.network。我有一个组织列表,这些组织通过父组织中的共同成员资格连接。 我在那个用二进制变量编码的组织中有成员资格。 我已经通过以下代码(来自Create adjacency matrix based on binary variable )基于这些数据成功创建了 sociomatrix 和 subsequent.network object:
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
我如何创建一个双模式网络,第一组边是组织在较大组织中的成员资格(由链接变量表示),第二组边是组织中个人的领导 position?
谢谢大家
由reprex package (v0.3.0) 创建于 2020-10-24
有许多不同的方法可以做你想做的事情。 我不知道有哪个 function 可以根据您拥有的数据神奇地创建一个 two-mode.network,因此下面的解决方案涉及一些数据操作。 我们首先创建一个带有节点的数据框,然后是另一个带有边的数据框。 然后使用节点和边作为输入来创建network
object。代码不言自明:
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
来更改节点/边的颜色、大小、形状等。
这是一个使用 package igraph
的示例。 首先,我们将network
object 转换为igraph
object。
library(igraph)
library(intergraph)
my_netgraph <- asIgraph(my_network)
可以使用V(my.netgraph)$attribute_name
评估节点的属性。 例如,让我们看看我们之前定义的 your.network 中的节点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)
这就是 .network 现在的样子:
现在让我们根据属性$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
object 的其他属性:
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"
您可以在本 iGraph 手册(第 10-11 页)上找到更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.