繁体   English   中英

创建一个包含所有可能交互的表(2向和3向)

[英]Create a table with all possible interactions (2-way and 3-way)

我将此线程称为Create表,其中包含R中一列的所有值对,计算唯一值交互表 - 宠物和房屋的案例了解如何创建双向交互表。 我怎么能在所有可能的情况下这样做? 此外,我想在这些箱子(组合)中找到发生频率和收入。

这是我的输入数据

   Customer      Product Revenue
1         A         Rice      10
2         A Sweet Potato       2
3         A       Walnut       4
4         B         Rice       3
5         B       Walnut       2
6         C       Walnut       3
7         C Sweet Potato       4
8         D         Rice       3
9         E Sweet Potato       4
10        F       Walnut       7
11        G         Rice       2
12        G Sweet Potato       3
13        H Sweet Potato       4
14        H       Walnut       6
15        I         Rice       2

DFI <- structure(list(Customer = c("A", "A", "A", "B", "B", "C", "C", 
"D", "E", "F", "G", "G", "H", "H", "I"), Product = c("Rice", 
"Sweet Potato", "Walnut", "Rice", "Walnut", "Walnut", "Sweet Potato", 
"Rice", "Sweet Potato", "Walnut", "Rice", "Sweet Potato", "Sweet Potato", 
"Walnut", "Rice"), Revenue = c(10, 2, 4, 3, 2, 3, 4, 3, 4, 7, 
2, 3, 4, 6, 2)), .Names = c("Customer", "Product", "Revenue"), row.names = c(NA, 
15L), class = "data.frame")

下面就来生成产品的所有组合代码Sweet Potato RiceWalnut

Combinations<-do.call(c,lapply(seq_along(unique(DFI$Product)), 
  combn, x = unique(DFI$Product), simplify = FALSE))

[[1]]
[1] "Rice"

[[2]]
[1] "Sweet Potato"

[[3]]
[1] "Walnut"

[[4]]
[1] "Rice"         "Sweet Potato"

[[5]]
[1] "Rice"   "Walnut"

[[6]]
[1] "Sweet Potato" "Walnut"      

[[7]]
[1] "Rice"         "Sweet Potato" "Walnut"      

这是根据产品类型组合的出现频率的预期输出数据:

  Combination Frequency
1           R         2
2           S         1
3           W         1
4         R,S         1
5         S,W         2
6         R,W         1
7       R,S,W         1

DFOUTa <- structure(list(Combination = c("R", "S", "W", "R,S", "S,W", "R,W", 
"R,S,W"), Frequency = c(2, 1, 1, 1, 2, 1, 1)), .Names = c("Combination", 
"Frequency"), row.names = c(NA, 7L), class = "data.frame")

这是我在垃圾箱中的收入的预期输出数据(即产品类型的组合):

  Combination Revenue
1           R       5
2           S       4
3           W       7
4         R,S       5
5         S,W      17
6         R,W       5
7       R,S,W      16

DFOUTb <- structure(list(Combination = c("R", "S", "W", "R,S", "S,W", "R,W", 
"R,S,W"), Revenue = c(5, 4, 7, 5, 17, 5, 16)), .Names = c("Combination", 
"Revenue"), row.names = c(NA, 7L), class = "data.frame")

我已手动生成以上数据。 我已经仔细检查以确保没有错误。

我不确定如何生成我正在寻找的两个输出。 我真诚地感谢任何帮助。 我更喜欢基于data.table的方法,因为我原始数据集中的数据大小。


PS:为简洁起见,我在输出文件中分别将产品名称RiceSweet PotatoWalnut缩短为RSW

这应该可以获得频率和收入 - 我假设您希望将每个客户的订单组合成一个组合:

require(data.table); setDT(DFI)

DFI[order(Product)
  ][, .(Combination= paste(Product, collapse=", "), Revenue = sum(Revenue)) , by=.(Customer)
  ][, .(.N, Revenue= sum(Revenue)), by=.(Combination)]

                  Combination N Revenue
1: Rice, Sweet Potato, Walnut 1      16
2:               Rice, Walnut 1       5
3:                       Rice 2       5
4:         Rice, Sweet Potato 1       5
5:       Sweet Potato, Walnut 2      17
6:               Sweet Potato 1       4
7:                     Walnut 1       7

您可能会发现一次查看每个链接语句有助于查看每个步骤中发生的情况。 我要提到的唯一具体事情是我们从DFI[order(Product)] ,以确保我们生成的组合是一致的,所以我们最终不会得到“Rice,Potato” “Potato,Rice”

我会做...

# spin off product table, assign abbreviations
prodDF = DFI[, .(Product = unique(Product))][, prod := substr(Product, 1, 1)]
DFI[prodDF, on=.(Product), prod := i.prod]

# spin off customer table, assign their bundles and revenues
custDF = DFI[order(prod), .(Bundle = toString(prod)), keyby=Customer]    
custDF[DFI[, sum(Revenue), by=.(Customer)], rev := i.V1]

# aggregate from customers to bundles
res = custDF[, .(.N, rev = sum(rev)), keyby=Bundle]

# clean up extra columns
DFI[, prod := NULL]

这使

    Bundle N rev
1:       R 2   5
2:    R, S 1   5
3: R, S, W 1  16
4:    R, W 1   5
5:       S 1   4
6:    S, W 2  17
7:       W 1   7

这与@ Mako的答案非常相似,但......

  1. 我的两个汇总在汇总收入时使用?GForce ,而Mako在客户层面的收入总和则不然。
  2. 这样就可以留下客户表,您可以检查或合并其他客户属性(如果有的话); 和产品表同上。

这些并没有真正使这个答案更好,只是不同。 尽管是GForce的事情,我的方式实际上可能会变得更慢,因为我将客户分组或合并三次与单一时间的答案相比。 而对于第二个问题,另一个答案可能是简单/个人品味的单行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM