1

r を使用して、属性として民族性を持つ個人の無向ネットワークを分析しています。ネクタイ アカウント テーブル、または「選好マトリックス」を作成したいと考えています。これは、民族性の値が両方の次元に配列された正方行列であり、各セルは、そのタイプの関係に対応するネクタイの数を示します。(したがって、これにより、あるグループが別のグループに引き分けになる確率を計算できますが、igraphのpreference.game関数の引数として使用したいだけです)。これが私が試したことです:

# I create a variable for ethnicity by assigning the names of my vertices to their corresponding ethnicities

   eth <- atts$Ethnicity[match(V(mahmudNet)$name,atts$Actor)] 

# I create an adjacency matrix from my network data

   mat <- as.matrix(get.adjacency(mahmudNet))

# I create the dimensions for my preference matrix from the Ethnicity values

   eth.value <- unique(sort(eth))

# I create an empty matrix using these dimensions

eth.mat <- array(NA,dim=c(length(eth.value),length(eth.value)))

# I create a function that will populate the empty cells of the matrix

for (i in eth.value){
  for (j in eth.value){
    eth.mat[i,j] <- sum(mat[eth==i,eth==j])
  }
 }

私の問題は最後にあると思います。セルにデータを入力する方法を R に指示する式を理解する必要があります。私が入れた表現は機能していないようですが、潜在的に私が行くことができるようにしたいです

a <- sum(mat[eth=="White", eth=="Black"])

そして、"a" は、白黒関係に対応する隣接行列内のすべてのセルの合計を返します。

これが私のデータのサンプルです:

# data frame with Ethnicity attributes:

                     Actor Ethnicity
1    Sultan Mahmud of Siak         2
2            Daeng Kemboja         1
3  Raja Kecik of Trengganu         1
4                Raja Alam         2
5                Tun Dalam         2
6                Raja Haji         1
7           The Suliwatang         1
8          Punggawa Miskin         1
9          Tengku Selangor         1
10        Tengku Raja Said         1
11         Datuk Bendahara         2
12                     VOC         3
13        King of Selangor         1
14        Dutch at Batavia         3
15            Punggawa Tua         2
16    Raja Tua Encik Andak         1
17      Raja Indera Bungsu         2
18         Sultan of Jambi         2
19            David Boelen         3
20        Datuk Temenggong         2
21      Punggawa Opu Nasti         1

# adjacency matrix with relations

                  Daeng Kemboja Punggawa Opu Nasti Raja Haji Daeng Cellak
Daeng Kemboja                  0                  1         1            1
Punggawa Opu Nasti             1                  0         1            0
Raja Haji                      1                  1         0            0
Daeng Cellak                   1                  0         0            0
Daeng Kecik                    1                  0         0            0
                   Daeng Kecik
Daeng Kemboja                1
Punggawa Opu Nasti           0
Raja Haji                    0
Daeng Cellak                 0
Daeng Kecik                  0
4

1 に答える 1