2017-09-07 84 views
0

您好我有定購的雜貨的稀疏數據幀像這樣在arules中如何將稀疏數據框轉換爲事務?

library(arules) 
a_df <- data.frame(
apple = as.factor(c(1,0,0,0,1,1)), 
banana = as.factor(c(0,1,1,0,0,0)), 
peeler = as.factor(c(1,0,0,0,1,1))) 

a_tran = as(a_df, "transactions") 
inspect(a_tran) 
rules <- apriori(a_tran, parameter=list(minlen=2, supp=0.5,conf = 0.5)) 
inspect(rules) 

但是結果包括0(該項目不排序)如下: LHS RHS支持的信心提升計數 {香蕉= 0} => {蘋果= 1} 0.5 0.6 1.2 3

如何可以忽略在數據幀的0,或變換數據幀到像

order 1: apple, peeler 
order 2: banana 

感謝。

回答

0

這裏有幾個選項

library(magrittr) 
idx <- which(a_df==1, arr.ind = T) 
(lst <- split(names(a_df)[idx[,2]], idx[,1])) 
# $`1` 
# [1] "apple" "peeler" 
# 
# $`2` 
# [1] "banana" 
# 
# $`3` 
# [1] "banana" 
# 
# $`5` 
# [1] "apple" "peeler" 
# 
# $`6` 
# [1] "apple" "peeler" 

rules <- function(x, app=NULL) { 
    x %>% as("transactions") %>% apriori(parameter=list(minlen=2, supp=0.5,conf = 0.5), appearance=app) 
} 
# use a list without "0"s: 
lst %>% rules %>% inspect 
# filter "0"s afterwards: 
a_df %>% rules %>% subset(!lhs%pin%"0" & !rhs%pin%"0") %>% inspect 
# filter "0"s in apriori: 
a_df %>% rules(list(none = paste(names(a_df), "0", sep="="), default="both")) %>% inspect 
+0

謝謝!你是金子!從來不知道%pin%存在 – santoku

1

看起來像你的數據是一個完整的0-1矩陣。這裏是最快的方式:

trans <- as(a_df == "1", "transactions") 
inspect(trans) 

    items   
[1] {apple,peeler} 
[2] {banana}  
[3] {banana}  
[4] {}    
[5] {apple,peeler} 
[6] {apple,peeler} 

現在你可以挖掘規則。

+0

哇!謝謝。這真的很整潔 – santoku

相關問題