2016-09-28 137 views
-1

我想在R程序中使用apriori算法運行關聯規則模型。我有我的數據在txt文件格式或csv文件格式。我的數據一般看起來像這樣。關聯規則數據格式

a, b, c, d, 
e, f, g, h, 
i, j, k, l, 
(etc.) 

的數據通常被讀入R

read.transactions("Trial.txt") 
transactions in sparse format with 
138 transactions (rows) and 
217 items (columns) 

然而,當我嘗試運行先驗算法,我得到一個錯誤。

> m1 <- apriori("Trial.txt") 
Error in as(data, "transactions") : 
    no method or default for coercing 「character」 to 「transactions」 

我想有一個與我如何將數據輸入R.我曾嘗試刪除重複的方法,這似乎並沒有工作的問題。我應該如何將這些數據輸入到R來運行模型?

回答

0

參見文檔?apriori:第一個參數(data)預計將是一個「類的交易或任何數據結構可被強制轉換爲交易的對象」。您提供了長度爲1的字符向量,該向量不能被強制轉換爲事務對象。下面是一個例子:

writeLines("a,b,c,d 
e,f,g,h 
i,j,k,l", tf <- tempfile()) 
library(arules) 
(trans <- read.transactions(tf, sep=",")) 
# transactions in sparse format with 
# 3 transactions (rows) and 
# 12 items (columns) 
m1 <- apriori(trans, parameter = list(confidence = 1, minlen = 4)) 
head(inspect(m1)) 
#  lhs rhs support confidence lift 
# 1 {a,b,c} => {d} 0.3333333   1 3 
# 2 {a,b,d} => {c} 0.3333333   1 3 
# 3 {a,c,d} => {b} 0.3333333   1 3 
# 4 {b,c,d} => {a} 0.3333333   1 3 
# 5 {e,f,g} => {h} 0.3333333   1 3 
# 6 {e,f,h} => {g} 0.3333333   1 3