[英]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.