建筑物和邻接矩阵 [英] Building and adjacency matrix

查看:110
本文介绍了建筑物和邻接矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你们是否可以帮助我建立邻接矩阵.我有CVS格式的数据,如下所示:

I was wondering if you guys can help me building an adjacency matrix. I have data in CVS format like this:

Paper_ID    Author
2   Foster-McGregor, N.
3   Van Houte, M.
4   van de Meerendonk, A.
5   Farla, K.
6   van Houte, M.
6   Siegel, M.
8   Farla, K.
11  Farla, K.
11  Verspagen, B.

如您所见,"Paper_ID"列的重复值为11,表示"Farla,K".和"Verspagen,B."是出版物的合著者.我需要使用作者的姓名来构建平方加权矩阵,计算出他们共同协作的次数.

As you can see the column "Paper_ID" has a repeated value of 11, meaning that "Farla, K." and "Verspagen, B." are coauthors of a publication. I need to build a square weighted matrix using the names of the authors, counting the times that they are collaborating together.

推荐答案

以下内容是您要找的吗?

Does the following do what you are looking for?

# simulate data.
d <- data.frame(
  id=c(2,3,4,5,6,6,8,11,11,12,12),
  author=c("FN", "VM","VA","FK","VM","SM","FK","FK","VB","FK","VB")
)

d
   id author
1   2     FN
2   3     VM
3   4     VA
4   5     FK
5   6     VM
6   6     SM
7   8     FK
8  11     FK
9  11     VB
10 12     FK
11 12     VB

# create incidence matrix:
m <- xtabs(~author+id,d)
m
      id
author 2 3 4 5 6 8 11 12
    FK 0 0 0 1 0 1  1  1
    FN 1 0 0 0 0 0  0  0
    SM 0 0 0 0 1 0  0  0
    VA 0 0 1 0 0 0  0  0
    VB 0 0 0 0 0 0  1  1
    VM 0 1 0 0 1 0  0  0

# convert to adjacency matrix.
# tcrossprod does "m %*% t(m)"
tcrossprod(m)
      author
author FK FN SM VA VB VM
    FK  4  0  0  0  2  0
    FN  0  1  0  0  0  0
    SM  0  0  1  0  0  1
    VA  0  0  0  1  0  0
    VB  2  0  0  0  2  0
    VM  0  0  1  0  0  2

请注意,crossprod()将为您提供id变量的关联矩阵(即将为t(m) %*% m).

Note that crossprod() will give you the incidence matrix for the id variable (i.e. will do t(m) %*% m).

这篇关于建筑物和邻接矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆