本文介绍了如何计算表中的共现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的矩阵,例如

I have a simple matrix, e.g.

test <- matrix(c("u1","p1","u1","p2","u2","p2","u2",
                 "p3","u3","p1","u4","p2","u5","p1",
                 "u5","p3","u6","p3","u7","p4","u7",
                 "p3","u8","p1","u9","p4"),
               ncol=2,byrow=TRUE) 
colnames(test) <- c("user","product")
test1<-as.data.frame(test)

测试:

   user   product
1  u1      p1
2  u1      p2
3  u2      p2 
4  u2      p3
5  u3      p1
6  u4      p2
7  u5      p1
8  u5      p3
9  u6      p3
10 u7      p4
11 u7      p3
12 u8      p1
13 u9      p4

我想统计有多少用户一起购买了产品对,例如 p1&p2、p2&p3...

I want to count how many users bought product pair together, such as p1&p2, p2& p3...

table(test1$product,test1$product) 给我这个:

     p1   p2  p3  p4
 p1   4   0   0   0
 p2   0   3   0   0
 p3   0   0   4   0
 p4   0   0   0   2

我怎样才能得到正确的结果:

How can I get the right result as :

     p1   p2  p3  p4
 p1   4   1   1   0
 p2   1   3   1   0
 p3   1   1   4   1
 p4   0   0   1   2

推荐答案

看着你想要的输出,你正在寻找 crossprod 函数:

Looking at your desired output, you are looking for the crossprod function:

crossprod(table(test1))
#        product
# product p1 p2 p3 p4
#      p1  4  1  1  0
#      p2  1  3  1  0
#      p3  1  1  4  1
#      p4  0  0  1  2

这与 crossprod(table(test1$user, test1$product)) 相同(反映 Dennis 的评论).

This is the same as crossprod(table(test1$user, test1$product)) (reflecting Dennis's comment).

这篇关于如何计算表中的共现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 20:23