使用R中一列的所有值对创建表,计算唯一值

use*_*982 6 r reshape

我有数据显示客户购买了某些商品.他们可以多次购买商品.我需要的是一张表格,其中显示了所有可能的项目成对组合以及购买该组合的客户的唯一数量(表格的对角线将是购买每个项目的唯一人数).

这是一个例子:

item <- c("h","h","h","j","j")
customer <- c("a","a","b","b","b")
test.data <- data.frame(item,customer)
Run Code Online (Sandbox Code Playgroud)

这是test.data:

item customer
h    a
h    a
h    b
j    b
j    b
Run Code Online (Sandbox Code Playgroud)

需要的结果 - 包含行和列名称的项目的表格,以及在表格内购买该对的唯一客户的计数.因此,2个客户购买了商品h,1个购买了商品h和j,1个购买了商品j.

item   h    j
h      2    1
j      1    1
Run Code Online (Sandbox Code Playgroud)

我已经尝试过使用表函数melt/ cast等,但没有任何东西能让我获得表中所需的计数.我的第一步是使用unique()去掉重复的行.

Chr*_*ris 5

使用data.tablegtools包装,我们可以重新创建客户所有可能的排列:

library(data.table)
library(gtools)

item <- c("h","h","h","j","j")
customer <- c("a","a","b","b","b")
test.data <- data.table(item,customer)

DT <- unique(test.data) #The unique is used as multiple purchases do not count twice

tuples <- function(x){
  return(data.frame(permutations(length(x), 2, x, repeats.allowed = T, set = F), stringsAsFactors = F))
}

DO <- DT[, tuples(item), by = customer]
Run Code Online (Sandbox Code Playgroud)

这给出了:

   customer X1 X2
1:        a  h  h
2:        b  h  h
3:        b  h  j
4:        b  j  h
5:        b  j  j
Run Code Online (Sandbox Code Playgroud)

这是客户拥有的所有唯一商品配对的列表.根据你的例子,我们以不同于jx h的方式处理hxj.我们现在可以使用表函数获取每对的频率:

table(DO$X1,DO$X2)
    j h
  j 1 1
  h 1 2
Run Code Online (Sandbox Code Playgroud)


Fra*_*ank 5

这是一个基本的R解决方案:

n_intersect <- Vectorize( function(x,y) length(intersect(x,y)) )

cs_by_item <- with(test.data, tapply(customer, item, unique))

outer(cs_by_item , cs_by_item , n_intersect)
#   h j
# h 2 1
# j 1 1
Run Code Online (Sandbox Code Playgroud)