生成唯一用户项跨产品组合的矩阵

pra*_*ara 2 combinations r unique matrix cross-product

我正在尝试在R中创建一个独特用户的跨产品矩阵.我在SO上搜索它但找不到我想要的东西.任何帮助表示赞赏.我有一个大型数据框(超过一百万),并显示了一个示例:

df <- data.frame(Products=c('Product a', 'Product b', 'Product a', 
                            'Product c', 'Product b', 'Product c'),
                 Users=c('user1', 'user1', 'user2', 'user1', 
                         'user2','user3'))
Run Code Online (Sandbox Code Playgroud)

df的输出是:

   Products Users
1 Product a user1
2 Product b user1
3 Product a user2
4 Product c user1
5 Product b user2
6 Product c user3
Run Code Online (Sandbox Code Playgroud)

我想看到两个矩阵:第一个将显示具有任一产品(OR)的唯一用户的数量 - 因此输出将类似于:

            Product a   Product b   Product c
Product a                 2            3
Product b     2                        3
Product c     3           3 
Run Code Online (Sandbox Code Playgroud)

第二个矩阵将是具有两个产品(AND)的唯一用户数:

            Product a   Product b   Product c
Product a                 2            1
Product b     2                        1
Product c     1           1 
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

谢谢

更新:

这里更清晰:产品a由User1和User2使用.产品b由User1和User2使用,产品c由User1和User3使用.因此,在第一个矩阵中,产品a和产品b将是2,因为有2个唯一用户.类似地,产品a和产品c将是3.在第二个矩阵中,它们将是2和1,因为我想要交叉点.谢谢

akr*_*run 5

尝试

lst <- split(df$Users, df$Products)
ln <- length(lst)
m1 <-  matrix(0, ln,ln, dimnames=list(names(lst), names(lst)))
m1[lower.tri(m1, diag=FALSE)] <- combn(seq_along(lst), 2, 
               FUN= function(x) length(unique(unlist(lst[x]))))
m1[upper.tri(m1)] <- m1[lower.tri(m1)]
m1
#          Product a Product b Product c
#Product a         0         2         3
#Product b         2         0         3
#Product c         3         3         0
Run Code Online (Sandbox Code Playgroud)

或使用 outer

f1 <- function(u, v) length(unique(unlist(c(lst[[u]], lst[[v]]))))
res <- outer(seq_along(lst), seq_along(lst), FUN= Vectorize(f1)) *!diag(3)
dimnames(res) <- rep(list(names(lst)),2)
res
#          Product a Product b Product c
#Product a         0         2         3
#Product b         2         0         3
#Product c         3         3         0
Run Code Online (Sandbox Code Playgroud)

对于第二种情况

tcrossprod(table(df))*!diag(3)
#            Products
#Products    Product a Product b Product c
# Product a         0         2         1
# Product b         2         0         1
# Product c         1         1         0
Run Code Online (Sandbox Code Playgroud)