2017-06-12 63 views
0

如果我有通用的訂單交易的表[R如何獲得每個訂單的產品配對數量?

order_id product_id value 
1000  A   100 
1000  C   55 
1000  D   75 
1001  B   85 
1001  A   35 
1001  D   75 
1002  B   70 
1002  E   20 

structure(list(order_id = c(1000L, 1000L, 1000L, 1001L, 1001L, 
1001L, 1002L, 1002L), product_id = structure(c(1L, 3L, 4L, 2L,1L, 4L, 2L, 5L), 
.Label = c("A", "B", "C", "D", "E"), class = "factor"), 
value = c(100L, 55L, 75L, 85L, 35L, 75L, 70L, 20L)), .Names = c("order_id","product_id", "value"), 
class = "data.frame", row.names = c(NA, -8L)) 

我該如何獲得數量和/或平均/累計值的產品配對過ORDER_ID,如:

product_id_one product_id_two  count 
A     B     1 
A     C     1 
A     D     2 
A     E     0 
B     C     0 
B     D     1 
B     E     1 
C     D     1 
C     E     0 
D     E     0 

product_id_one product_id_two  value_average 
A     B     175 
A     C     55 
A     D     142.5 
A     E     0 
B     C     0 
B     D     160 
B     E     90 
C     D     130 
C     E     0 
D     E     0 

除了只是循環它或一些類似的迭代方法?產品ID的順序應該不重要。

+0

是不是3 order_id – akrun

+0

我的目標是統計一個配對出現在唯一順序id中的數量,而不是order_ids本身的數量,即「產品A和B多久一起訂購」 –

+0

我認爲預期產出你根據公認的答案顯示不正確 – akrun

回答

1

我的解決方案(修訂版)

require(data.table) 
mydf <- structure(list(order_id = c(1000L, 1000L, 1000L, 1001L, 1001L, 
           1001L, 1002L, 1002L), product_id = structure(c(1L, 3L, 4L, 2L, 
                       1L, 4L, 2L, 5L), .Label = c("A", "B", "C", "D", "E"), class = "factor"), 
        value = c(100L, 55L, 75L, 85L, 35L, 75L, 70L, 20L)), .Names = c("order_id", 
                        "product_id", "value"), class = "data.frame", row.names = c(NA, 
                                       -8L)) 
mydf <- data.table(mydf,key="order_id") 
mydf2 <- mydf[mydf,allow.cartesian=TRUE] 
mydf2 <- mydf2[product_id!=i.product_id] 
mydf2[,idx:=.I] 
mydf2[,firstsecond:=paste0(min(as.character(product_id),as.character(i.product_id)),"_",max(as.character(product_id),as.character(i.product_id))),by=idx] 
mydf2 <- mydf2[,.N,by=.(firstsecond,order_id,value)][,N:=NULL] 
mydf3 <- mydf2[,.(count=length(unique(order_id)),value_average=sum(value)/length(unique(order_id))),by=firstsecond] 
mydf3[,c("product1","product2"):=tstrsplit(firstsecond,"_")] 
# firstsecond count value_average product1 product2 
# 1:   A_C  1   155.0  A  C 
# 2:   A_D  2   142.5  A  D 
# 3:   C_D  1   130.0  C  D 
# 4:   A_B  1   120.0  A  B 
# 5:   B_D  1   160.0  B  D 
# 6:   B_E  1   90.0  B  E 

讓我知道如果這能解決你的問題。

+0

這使我更進一步。兩個較小的問題: •重複列出(AD&DA)和•這是每個訂單的平均價格的平均值(即(100 + 75 + 35 + 75)/ 4),而不是每個訂單的平均值((175 + 110 )/ 2);我可以弄清楚前者,但你如何解決後者? –

+0

@KarstenSender我已經更新瞭解決方案。讓我知道它是否有效。 –

+0

非常完美。非常感謝,我將通過代碼工作。 –

0

從您給出的示例數據中,我看不到product_id_one:A和product_id_two:B之間的任何關聯與計數或平均值之間的關聯。你能否添加更多的細節?

否則,假設您想要按每個order_id聚合,我可以建議使用data.table。

library(data.table) 
library(dplyr) 
# 
dd <- data.frame(order_id = c(1000, 1000, 1000, 1001, 1001, 1001, 1002, 1002), 
       product_id = c('A', 'C', 'D', 'B', 'A', 'D', 'B', 'E'), 
       value = c(100, 55, 75, 85, 35, 75, 70, 20)) 
# 
setDT(dd) 
dgrouped <- dd %>% group_by(order_id, product_id) %>% summarize(count = n(), value_average = mean(value)) 
# 

如果您正在尋找關聯,你可能想看看apriori算法,arules包。

相關問題