创建共生矩阵

创建共生矩阵

我正在尝试解决共同出现矩阵的问题。我有一个事务和项目的数据文件,我想看到一起显示项目的事务数量的矩阵。

我是R编程的新手,我很乐意找到R所有的快捷方式,而不是创建特定的循环(我以前使用的是C年前,现在只坚持使用Excel宏和SPSS)。我已经检查过这里的解决方案,但是没有找到一个有效的方法(最接近的是这里给出的解决方案:使用SAC的共生矩阵? - 但是当我使用projection_tm时它产生了一条错误信息,我怀疑cbind不是'在我的案例中成功。

基本上我有一个包含以下内容的表:

TrxID Items Quant

Trx1 A 3

Trx1 B 1

Trx1 C 1

Trx2 E 3

Trx2 B 1

Trx3 B 1

Trx3 C 4

Trx4 D 1

Trx4 E 1

Trx4 A 1

Trx5 F 5

Trx5 B 3

Trx5 C 2

Trx5 D 1, etc.

我想创建类似的东西:


   A B C D E F

A  0 1 1 0 1 1

B  1 0 3 1 1 0

C  1 3 0 1 0 0

D  1 1 1 0 1 1

E  1 1 0 1 0 0

F  0 1 1 1 0 0

我做的是(你可能会笑我的菜鸟R方法):


library(igraph)

library(tnet)


trx <- read.table("FileName.txt", header=TRUE) 

transID <- t(trx[1])

items <- t(trx[2])


id_item <- cbind(items,transID)

item_item <- projecting_tm(id_item, method="sum")

item_item <- tnet_igraph(item_item,type="weighted one-mode tnet")

item_matrix <-get.adjacency(item_item,attr="weight")

item_matrix

如上所述,cbind可能不成功,所以projection_tm无法给我任何结果。


任何替代方法或我的方法的更正?


非常感谢您的帮助!


月关宝盒
浏览 607回答 3
3回答

米琪卡哇伊

使用上述任一答案中的“dat”,尝试crossprod并table:V <- crossprod(table(dat[1:2]))diag(V) <- 0V#&nbsp; &nbsp; &nbsp; Items# Items A B C D E F#&nbsp; &nbsp; &nbsp;A 0 1 1 1 1 0#&nbsp; &nbsp; &nbsp;B 1 0 3 1 1 1#&nbsp; &nbsp; &nbsp;C 1 3 0 1 0 1#&nbsp; &nbsp; &nbsp;D 1 1 1 0 1 1#&nbsp; &nbsp; &nbsp;E 1 1 0 1 0 0#&nbsp; &nbsp; &nbsp;F 0 1 1 1 0 0
打开App,查看更多内容
随时随地看视频慕课网APP